Monday, May 6, 2013

Write to separate Log files using Logback

There may be many instance you may have to write your logs to separate log files. Here are the easy steps to do that


  • Write a new logger in the existing log configuration file
<!-- Plain Text Rolling Appender for Custom logger file -->
    <appender name="SEPERATE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Append>true</Append>
        <File>seperate.log</File>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>seperate.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>
    
    <!-- additivity=false ensures seperate ata only goes to the seperate log -->
    <logger name="seperate" level="DEBUG" additivity="false">
        <appender-ref ref="SEPERATE_FILE"/>
    </logger>

  • Add the new logger where every you need in the java class as 
private static final Logger log = LoggerFactory.getLogger("seperate");
  • Starts logging as log.info..., log.debug..
  • Cool ha

No comments:

Post a Comment