1 Reply Latest reply on Oct 7, 2014 6:47 PM by James Perkins

    Logging to mulitiple files with slf4j and jboss logging in eap 6.3

    Benjamin Hundley Newbie

      I am using slf4j in my java web application.  I have declared the slf4j dependency in my maven with a provided scope.  My understanding is that JBoss will detect the slf4j logger and bind it with it's native org.jboss.logging system.  So far it's working.  It's writing to the console and the server.log file.  What I want to know is how I direct different logging messages to different files using this logging configuration.

       

      Within my application I am using this line to get the logger.

       

      final Logger logger = LoggerFactory.getLogger(MyClass.class);

       

      My logging configuration within my standalone.xml file is:

       

      <subsystem xmlns="urn:jboss:domain:logging:1.0">

         <console-handler name="CONSOLE" autoflush="true">

             <level name="DEBUG"/>

             <formatter>

                 <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

             </formatter>

         </console-handler>

         <periodic-rotating-file-handler name="FILE" autoflush="true">

             <formatter>

                 <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

             </formatter>

             <file relative-to="jboss.server.log.dir" path="server.log"/>

             <suffix value=".yyyy-MM-dd"/>

         </periodic-rotating-file-handler>

         <periodic-rotating-file-handler name="FILE2" autoflush="true">

             <formatter>

                 <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

             </formatter>

             <file relative-to="jboss.server.log.dir" path="server2.log"/>

             <suffix value=".yyyy-MM-dd"/>

         </periodic-rotating-file-handler>

         <logger category="com.arjuna">

             <level name="WARN"/>

         </logger>

         [...]

         <root-logger>

             <level name="DEBUG"/>

             <handlers>

                 <handler name="CONSOLE"/>

                 <handler name="FILE"/>

                 <handler name="FILE2"/>

             </handlers>

         </root-logger>

      </subsystem>

       

      So my question is, how do I specify within my program which file handler I write to?

       

      Thanks in advance.

      Ben

        • 1. Re: Logging to mulitiple files with slf4j and jboss logging in eap 6.3
          James Perkins Master

          You just need to define a logger in the logging subsystem and assign your handler to it. Let's assume you're class is named com.example.MyClass. You can define either a logger called com.example or com.example.MyClass. Defining it as com.example is generally the best approach unless you want that specific logger to go it it's own file.

           

          CLI Example:

          /subsystem=logging/logger=com.example:add(handlers=[FILE2])
          

           

          If you want com.example to only log to the FILE2 handler and not the CONSOLE or FILE handlers, then add the use-parent-handlers=false attribute.

          /subsystem=logging/logger=com.example:add(handlers=[FILE2], use-parent-handlers=false)
          

           

           

          --

          James R. Perkins