Version 1

    JBoss log manager currently doesn't support filtering logs on the basis of Exceptions thrown. Currently it's  not possible to use a regex filter as the stack trace is not used in when testing for matches. So only way we can filter exception logs is by writing our own custom handlers.

     

    Here is an example of how you can create a custom handler to filter Exception logs. All custom handlers must extend java.util.logging.Handler. For this example I am extending org.jboss.logmanager.handlers.PeriodicRotatingFileHandler as it would help me leverage some of the features PeriodicRotatingFileHandler provides.

     

    package org.jboss.logmanager.handlers;
    
    import org.jboss.logmanager.ExtLogRecord;
    
    public class ExceptionHandler extends PeriodicRotatingFileHandler{
    
        private String classname; //New property name which can be passed to custom handler for filtering purpose
    
        public void setClassname(String classname) {
            this.classname = classname;
        }
    
        /** {@inheritDoc} 
            Overriding doPublish methods
        */
        protected void doPublish(final ExtLogRecord record) {
    
            if(record.getThrown()!=null){ //record.getThrown won't be null if event involved is an exception else null.
                if(record.getThrown().getClass().toString().contains(classname))
                    super.doPublish(record);
            }
    
        }
    
    
    
    
    
    
    
    
    }
    

     

    Here in this custom handler we would be overriding doPublish method to check if the event involved is an exception, if it's an exception then we go forward to check the exception thrown which needs to be filtered.

     

    Once custom handler is created we will have to package the class into a JAR file (jboss-exceptionhandler.jar) and create a JBoss module. Module.xml would look like below.

     

    <?xml version='1.0' encoding='UTF-8'?>
    
    <module xmlns="urn:jboss:module:1.1" name="org.jboss.exceptionhandler">
    
        <resources>
            <resource-root path="jboss-exceptionhandler.jar"/>
        </resources>
    
        <dependencies>
            <module name="javax.api"/>
            <module name="org.jboss.modules"/>
        </dependencies>
    </module>
    
    
    
    
    

     

    You can create  the module using JBoss CLI or you can create it manually by JAR and module.xml at correct location. Please refer Wildfly documentation for more details about creating modules.

     

    Once module is created successfully, we will have to configure custom handler under logging subsystem of your configuration. Configuration XML would look like

     

               <custom-handler name="ExceptionStack" class="org.jboss.logmanager.handlers.ExceptionHandler" module="org.jboss.exceptionhandler">  
                    <level name="INFO"/>  
                    <formatter>  
                        <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>  
                    </formatter>  
                    <properties>
                        <property name="fileName" value="${jboss.server.log.dir}/exceptions.log"/>
                        <property name="suffix" value=".yyyy-MM-dd"/>
                        <property name="classname" value="StartException"/>
                        <property name="append" value="true"/>
                        <property name="autoFlush" value="true"/>
                    </properties>
                </custom-handler> 
    

     

    Here classname is the new property created in the custom handler which can be used to pass exception type which needs to be filtered. For example if you want to filter all null pointer exceptions then you can pass "NullPointerException" as value for the property. Once custom handler is configured you add this handler to root logger or any other logger of your choice.

     

                 <root-logger>
                     <level name="INFO"/>
                     <handlers>
                        <handler name="CONSOLE"/>
                        <handler name="FILE"/>
                        <handler name="ExceptionStack"/>
                    </handlers>
                </root-logger>
    

     

    This is just an example of how you can use custom handlers in JBoss logmanager to filter exception logs. You can explore other ways for achieving the results.