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">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n</pattern>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <!-- additivity=false ensures seperate ata only goes to the seperate log -->
    <logger name="seperate" level="DEBUG" additivity="false">
        <appender-ref ref="SEPERATE_FILE"/>

  • 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.debug..
  • Cool ha

No comments:

Post a Comment