[logback-user] Best Practice to Setup Multiple Log Files for Multiple Classes

Dan Franks dan.franks at gmail.com
Thu Oct 11 01:29:23 CEST 2012


I was trying this but then all the logging from all the classes wrote
to all the files.


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

<configuration scan="true" scanPeriod="30 seconds">


      <appender name="com.myCompany.orangeClassFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
      <append>true</append>
      <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>info</level>
      </filter>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>c.m.orangeClass.%d{yyyy-MM-dd}</fileNamePattern>
      </rollingPolicy>

              <layout class="ch.qos.logback.classic.PatternLayout">
          <pattern>%d{yyyy-MM-dd HH:mm:ss}, %p, %c, %t, %C{1}, %M %m%n</pattern>
        </layout>
                </appender>






      <appender name="com.myCompany.purpleClassFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
      <append>true</append>
      <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>info</level>
      </filter>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>c.m.purpleClass.%d{yyyy-MM-dd}</fileNamePattern>
      </rollingPolicy>

              <layout class="ch.qos.logback.classic.PatternLayout">
          <pattern>%d{yyyy-MM-dd HH:mm:ss}, %p, %c, %t, %C{1}, %M %m%n</pattern>
        </layout>
                </appender>






      <appender name="com.myCompany.greenClassFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
      <append>true</append>
      <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>debug</level>
      </filter>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>c.m.greenClass.Loader.%d{yyyy-MM-dd}</fileNamePattern>
      </rollingPolicy>

              <layout class="ch.qos.logback.classic.PatternLayout">
          <pattern>%d{yyyy-MM-dd HH:mm:ss}, %p, %c, %t, %C{1}, %M %m%n</pattern>
        </layout>
                </appender>







  <appender name="RootFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
    <append>true</append>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>info</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>c.m.default.%d{yyyy-MM-dd}</fileNamePattern>
    </rollingPolicy>

          <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>%d{yyyy-MM-dd HH:mm:ss}, %p, %c, %t, %C{1}, %M %m%n</pattern>
      </layout>
          </appender>





 <logger additivity="false" name="com.myCompany.orangeClass">
 <level value="info"/>
    <appender-ref ref="com.myCompany.orangeClassFileAppender"/>
     </logger>
 <logger additivity="false" name="com.myCompany.purpleClass">
 <level value="info"/>
    <appender-ref ref="com.myCompany.purpleClassFileAppender"/>
     </logger>
 <logger additivity="false" name="com.myCompany.greenClass">
 <level value="debug"/>
    <appender-ref ref="com.myCompany.greenClassFileAppender"/>
     </logger>

<root><level value="info"/>
    <appender-ref ref="RootFileAppender"/>
    </root>

</configuration>


More information about the Logback-user mailing list