1 2 Previous Next 20 Replies Latest reply on Nov 6, 2013 7:57 PM by James Perkins

    Logging filter in Jboss as 7.1.1 Final

    Piyush Jain Newbie

      Hi,

       

      We are migrating our application from Jboss 4.2.2 GA to Jboss 7.1.1 Final.

       

      We had filter in logging in Jboss 4.2.2 GA.

       

      Settings in Jboss 4.2.2 GA

       

      Inside server/default/conf/jboss-log4j/xml

       

      <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">

              <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler" />

              <param name="File" value="${jboss.server.log.dir}/server.log" />

              <param name="Append" value="false" />

              <!-- Rollover at midnight each day -->

              <param name="DatePattern" value="'.'yyyy-MM-dd" />

              <!-- Rollover at the top of each hour

              <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>

              -->

              <layout class="org.apache.log4j.EnhancedPatternLayout">

                  <!-- For more on the conversion pattern, see: http://logging.apache.org/log4j/companions/extras/apidocs/index.html -->

                 

                  <param name="ConversionPattern" value="%d{ABSOLUTE}{@@TimeZone@@} %-5p [%c] %m%n" />

              </layout>

       

              <filter class="com.log4jextensions.filter.hierarchical.HierarchicalExceptionFilter">

       

                  <param name="rulesFile" value="jbossHierarchicalExceptionFilter.xml" />

       

                  <param name="enabled" value="true" />

       

                  <param name="logWhenHandled" value="true" />

       

                  <param name="logWhenNotHandled" value="false" />

       

              </filter>

          </appender>

       

      Attached are the HierarchicalExceptionFilter Java and jbossHierarchicalExceptionFilter xml files.

       

      Problem in Jboss as 7

       

      I want to migrate the same in Jboss as 7 but came to know that Jboss as 7 does not support filter with class attribute  ( XSD does not allow ).

       

      please find error below while starting the server

       

      "Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[78,3]

      Message: JBAS014788: Unexpected attribute 'class' encountered

          at org.jboss.as.controller.parsing.ParseUtils.unexpectedAttribute(ParseUtils.java:104) [jboss-as-controller-7.1.1.Final.jar:7.1.1.Final]

          at org.jboss.as.logging.LoggingSubsystemParser.parseFilter(LoggingSubsystemParser.java:938)

          at org.jboss.as.logging.LoggingSubsystemParser.parsePeriodicRotatingFileHandlerElement(LoggingSubsystemParser.java:675)

          at org.jboss.as.logging.LoggingSubsystemParser.readElement(LoggingSubsystemParser.java:173)

          at org.jboss.as.logging.LoggingSubsystemParser.readElement(LoggingSubsystemParser.java:113)

          at org.jboss.staxmapper.XMLMapperImpl.processNested(XMLMapperImpl.java:110) [staxmapper-1.1.0.Final.jar:1.1.0.Final] "

       

      Please show me some other alternative way.

       

       

      Thanks in advance,

      Piyush Jain

        • 1. Re: Logging filter in Jboss as 7.1.1 Final
          James Perkins Master

          Only specific filters are allowed in JBoss AS 7. You should be able to see them in the XSD.

           

          --

          James R. Perkins

          • 2. Re: Logging filter in Jboss as 7.1.1 Final
            Piyush Jain Newbie

            Thanks for the help.

             

            Can you kindly please give me an example of filter which changes the log level.

             

            I wrote below but it changes log level for all the message, I want to do sepecifically for some packages.

             

            <filter>

                 <any>

                      <change-level new-level = "ERROR" />

                      <match pattern = "JBAS016330" />

                 </any>

            </filter>

             

             

            Below are files which I used in Jboss-4.2.2 GA.  In Java class I used to do log it is INFO level.

             

            Configuration:

             

            <filter class="com.log4jextensions.filter.hierarchical.HierarchicalExceptionFilter">

             

                        <param name="rulesFile" value="jbossHierarchicalExceptionFilter.xml" />

             

                        <param name="enabled" value="true" />

             

                        <param name="logWhenHandled" value="true" />

             

                        <param name="logWhenNotHandled" value="false" />

             

                    </filter>

             

            XML: jbossHierarchicalExceptionFilter

             

            <?xml version="1.0" encoding="UTF-8"?>

            <HierarchicalExceptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="HierarchicalExceptionFilter.xsd">

             

               <!-- couldn't restore view -->

               <exception depth="any">

                  <exception match="javax.faces.application.ViewExpiredException" type="equals">

                     <exception depth="any"/>

                  </exception>

               </exception>

              

               <!-- thrown by Hibernate when row was updated or deleted by another transaction -->

               <exception match="org.hibernate.StaleObjectStateException" type="equals"/>

              

               <!-- thrown by FileUpload when it is cancelled/reset when in progress -->

               <exception match="org.jboss.seam.web.FileUploadException" type="equals"/>

              

               <!-- thrown by JSF when entity is not found -->

               <exception depth="any">

                  <exception match="javax.faces.el.EvaluationException" type="equals">

                     <exception match="javax.ejb.EJBTransactionRolledbackException" type="equals">

                        <exception match="javax.persistence.EntityNotFoundException" type="equals"/>

                     </exception>

                  </exception>

               </exception>

             

                <!-- thrown during DataTable operations -->

               <exception depth="any">

                  <exception match="com.openet.referenceDataGui.dbManagement.exceptions.DatabaseAccessException" type="equals">

                     <exception depth="any"/>

                  </exception>

               </exception>

             

            </HierarchicalExceptions>

             

            Java: HierarchicalExceptionFilter

             

            package com.log4jextensions.filter.hierarchical;

             

            import java.io.InputStream;

             

            import javax.xml.bind.JAXBContext;

            import javax.xml.bind.Unmarshaller;

            import javax.xml.validation.Schema;

            import javax.xml.validation.SchemaFactory;

             

            import org.apache.log4j.Logger;

            import org.apache.log4j.spi.Filter;

            import org.apache.log4j.spi.LoggingEvent;

             

            public class HierarchicalExceptionFilter extends Filter {

             

                private static final Logger LOGGER = Logger.getLogger(HierarchicalExceptionFilter.class);

               

                private static final String SCHEMA_FILE = "HierarchicalExceptionFilter.xsd";

                private static final String RULES_FILE = "hierarchicalExceptionFilter.xml";

                private static final Class<?>[] importClasses = new Class[]{HierarchicalExceptions.class, HierarchicalException.class};   

             

                private HierarchicalExceptions exceptions = new HierarchicalExceptions();

               

                private boolean initialzied = false;

                private boolean initialzieError = false;

                private String rulesFile = RULES_FILE;

                private boolean enabled = true;

                private boolean logWhenHandled = true;

                private boolean logWhenNotHandled = false;

               

                public HierarchicalExceptionFilter(){

                }

               

                public HierarchicalExceptionFilter(String rulesFile){

                    this.rulesFile = rulesFile;

                }

               

                private void initialize(){

                    SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

                    Schema schema = null;

                    try {

                        schema = sf.newSchema(getClass().getClassLoader().getResource(SCHEMA_FILE));

                    } catch (Exception e) {

                        initialzieError = true;

                        LOGGER.warn("Error parsing schema "+SCHEMA_FILE + " cause : "+e.getLocalizedMessage(), e);

                        return;

                    }

                    try

                    {

                        InputStream stream = getClass().getClassLoader().getResourceAsStream(rulesFile);

                        if (stream == null){

                            LOGGER.warn("Couldn't find "+rulesFile + " file.");

                            initialzieError = true;

                            return;

                        }

                        JAXBContext context = JAXBContext.newInstance(importClasses);

                        Unmarshaller unmarshaller = context.createUnmarshaller();

                        unmarshaller.setSchema(schema);

                        exceptions = (HierarchicalExceptions) unmarshaller.unmarshal(stream);

                    }catch (Exception e) {

                        initialzieError = true;

                        LOGGER.warn("Couldn't load "+rulesFile + " cause : "+e.getLocalizedMessage(), e);

                    }

                }

               

                @Override

                public int decide(LoggingEvent event) {

                    return decide(event, -1);

                }

               

                /**

                 * if index is -1 than check all, if >= 0 check at position

                 */

                public int decide(LoggingEvent event, int index) {

                    if (!enabled) {   

                        return ACCEPT;

                    }

                    if (!initialzieError) {

                        if (!initialzied ){

                            initialzied = true;

                            initialize();

                        }

                        if (initialzied) {

                            try {

                                if (event.getThrowableInformation() != null){

                                    if (check(event, index)) {

                                        if (logWhenHandled) {

                                            LOGGER.info("Found and handled "+exceptions.getInfo(event.getThrowableInformation()));

                                        }

                                        return DENY;

                                    }

                                    else {

                                        if (logWhenNotHandled) {

                                            LOGGER.info("Not handled "+exceptions.getInfo(event.getThrowableInformation()));

                                        }

                                        return ACCEPT;

                                    }

                                }

                            }

                            catch (Exception e) {

                                initialzieError = true;

                                LOGGER.error("Error during processing exceptions.", e);

                            }

                        }           

                    }

                    return ACCEPT;

                }

               

                private boolean check(LoggingEvent event, int index) {

                    if (index == -1) {

                        return exceptions.matches(event.getThrowableInformation());

                    }

                    else {

                        return exceptions.matches(event.getThrowableInformation(), index);

                    }

                }

             

                public void setRulesFile(String rulesFile) {

                    this.rulesFile = rulesFile;

                }

             

                public void setEnabled(boolean enabled) {

                    this.enabled = enabled;

                }

               

                public void setLogWhenHandled(boolean logWhenHandled) {

                    this.logWhenHandled = logWhenHandled;

                }

             

                public void setLogWhenNotHandled(boolean logWhenNotHandled) {

                    this.logWhenNotHandled = logWhenNotHandled;

                }

            }

            • 3. Re: Logging filter in Jboss as 7.1.1 Final
              Piyush Jain Newbie

              I saw that logging.properties allows old style filter with Java class.

               

              But by default logging.properties is active till only jboss starts. To make that configuration even after Jboss start I will have to remove logging subsystem from standalone.xml.

               

              Is it ok to remove logging subsystem and use logging.properties only?  are there some hidden problems which I am unable to see now?

              • 4. Re: Logging filter in Jboss as 7.1.1 Final
                James Perkins Master

                You could try something like:

                 

                <filter>
                    <all>
                        <match pattern="JBAS016330"/>
                        <change-level new-level="ERROR"/>
                    </all>
                </filter>
                

                 

                If you attach that to a specific category it should work. Note though that filters are not inherited so the category needs to be specific to the actual category being used.

                 

                --

                James R. Perkins

                • 5. Re: Logging filter in Jboss as 7.1.1 Final
                  James Perkins Master

                  You can absolutely remove the logging subsystem and just use the logging.properties configuration. The main issue with that is there will be no runtime logging changes. Any changes will require a restart.

                   

                  --

                  James R. Perkins

                  • 6. Re: Logging filter in Jboss as 7.1.1 Final
                    Piyush Jain Newbie

                    Can you kindly please accept JIRA for allowing filter with class attribute and param subtag as we have in Jboss 4.2.2 GA?

                     

                    <filter class="com.log4jextensions.filter.hierarchical.HierarchicalExceptionFilter">

                                <param name="rulesFile" value="jbossHierarchicalExceptionFilter.xml" />

                                <param name="enabled" value="true" />

                                <param name="logWhenHandled" value="true" />

                                <param name="logWhenNotHandled" value="false" />

                            </filter>

                    • 7. Re: Logging filter in Jboss as 7.1.1 Final
                      jaikiran pai Master

                      That looks like a log4j filter. I'm just curious - why not just package a log4j.xml within your application with the relevant configurations like this filter? The only disadvantage with that approach is that you won't be able to dynamically change the logging configuration of that application and instead would require a redeployment. Is that a concern for your application?

                      • 8. Re: Logging filter in Jboss as 7.1.1 Final
                        Piyush Jain Newbie

                        Yes we want logging configuration to be changable at runtime. Also we have multiple EARs so will have to change in each EARs if any.

                         

                        Is there alternative, we have applied our logic while filtering?

                        • 9. Re: Logging filter in Jboss as 7.1.1 Final
                          Piyush Jain Newbie

                          While I changed logging configuration runtime in logging subsystem, it does not come in effect.

                          We have to restart. As per documentation it should be changeable at runtime.

                           

                          Do i need to make some changes to enable this above feature?

                           

                           

                          Meanwhile I am able to have log4j as module. For multiple EARs we will have to change one file only.

                           

                          But there are two problems:

                           

                          1) Any change in log4j.xml requires restart. -- Can I do something to make changes effective runtime??

                          2) If there is any unhandled exception then that log message is going to file which is configured in logging subsystem. This is really frustating. How to have everything in one file which is mentioned in log4j ?

                           

                          Thanks & Regards,

                          Piyush Jain

                          • 10. Re: Logging filter in Jboss as 7.1.1 Final
                            James Perkins Master

                            Changes to the logging subsystem, minus I think one change to the asynch-handler, should take effect at runtime. If not, then there is probably a bug, but it's defintiely fixed in EAP 6.1.Alpha1. **Note I don't know of any bugs where runtime changes aren't taking effect for the logging subsystem.

                             

                            But there are two problems:

                             

                            1) Any change in log4j.xml requires restart. -- Can I do something to make changes effective runtime??

                             

                            I'm not a log4j expert, but I think there is a way to add some kind of configuration listener that will look for changes in the configuration.

                             

                             

                            2) If there is any unhandled exception then that log message is going to file which is configured in logging subsystem. This is really frustating. How to have everything in one file which is mentioned in log4j ?

                             

                            I'm not totally sure I understand here. I'm going to assume here you used a log4j configuration, excluded the server log4j dependency and provided your own log4j library. Anything logged in your application should go to the log4j file. That said anything loggers created by server modules will not use the log4j configuration.

                             

                            --

                            James R. Perkins

                            • 11. Re: Logging filter in Jboss as 7.1.1 Final
                              Piyush Jain Newbie

                              I have checked that changes in logging subsystem are not realtime for rotating file handler.  I am not sure if we have to modify some settings to make that happen. Can you kindly please check at your end?

                               

                              Also as requested earlier can you kindly please accept JIRA for allowing filter with class attribute and param subtag as we have in Jboss 4.2.2 GA?

                               

                              <filter class="com.log4jextensions.filter.hierarchical.HierarchicalExceptionFilter">

                                          <param name="rulesFile" value="jbossHierarchicalExceptionFilter.xml" />

                                          <param name="enabled" value="true" />

                                          <param name="logWhenHandled" value="true" />

                                          <param name="logWhenNotHandled" value="false" />

                                      </filter>

                              • 12. Re: Logging filter in Jboss as 7.1.1 Final
                                James Perkins Master

                                How are you making the changes? If you're editing the XML then that won't work at runtime and will likely be overwritten. You need to use a management interface, like CLI or the web console, to make the changes.

                                 

                                You can create a JIRA here https://issues.jboss.org/browse/AS7. Just set the component to logging and it will assign it to me. To be honest though I probably won't allow log4j filters to be used. We do allow, in EAP 6.1.Alpha1, log4j appenders to be used a custom handlers, but I'm not convinced enough to allow log4j filters or log4j error managers to be used.

                                 

                                --

                                James R. Perkins

                                • 13. Re: Logging filter in Jboss as 7.1.1 Final
                                  Piyush Jain Newbie

                                  Thanks James, By using management console I am able to change runtime.

                                   

                                  Can we use Absolute date format in Jboss logging also as we had in log4j?

                                   

                                  http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html  here you may find details.

                                  • 14. Re: Logging filter in Jboss as 7.1.1 Final
                                    James Perkins Master

                                    You can use any format that java.text.SimpleDateFormat would recognize. I think ABSOLUTE in log4j is HH:mm:ss,SSS.

                                     

                                    --

                                    James R. Perkins

                                    1 2 Previous Next