Thursday, August 8, 2013

Configure Logback for Web Applications



This is step by step guide to configure Logback as logging framework for your application

Step 1: Adding Dependencies

       <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>0.9.30</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.2</version>
        </dependency>

 Step 2: Placing logback.xml file for web applications

  Logback.xml file either can be placed at class-path or in resources folder of web application



 Step 3:  Configuration to write logs in console/File

Console

<?xml version="1.0"?>
<configuration>
     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
       <encoder>
              <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -                        %msg%n</pattern>
       </encoder>
     </appender>
     <root level="debug">
       <appender-ref ref="STDOUT" />
    </root>
</configuration>

* Log level can be setup to debug, info, error and warning depending on the need.

File

<?xml version="1.0"?>
<configuration>
       <appender name="FILE" class="ch.qos.logback.core.FileAppender">
              <file>C:\log4j\log.out</file>
              <append>true</append>     
              <encoder>
                     <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n
                     </pattern>
              </encoder>
       </appender>
       <root level="INFO">
              <appender-ref ref="FILE" />
       </root>
</configuration>

Writing to both File and Console simultaneously with different log level

Best way of achieving this functionality is by configuring root to print everything by setting log level to "trace", then apply filters at each appender level. This just works perfect

Here is the configuration file

<?xml version="1.0"?>
<configuration>
       <appender name="FILE" class="ch.qos.logback.core.FileAppender">
              <file>C:\log4j\log.out</file>
              <append>true</append>
              <filter class="ch.qos.logback.classic.filter.LevelFilter">
                     <level>ERROR</level>
                     <onMatch>ACCEPT</onMatch>
                     <onMismatch>DENY</onMismatch>
              </filter>
              <encoder>
                     <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n
                     </pattern>
              </encoder>
       </appender>
       <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
              <filter class="ch.qos.logback.classic.filter.LevelFilter">
                     <level>DEBUG</level>
                     <onMatch>ACCEPT</onMatch>
                     <onMismatch>DENY</onMismatch>
              </filter>
              <encoder>
                     <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
                     </pattern>
              </encoder>
       </appender>
      

       <root level="trace">
              <appender-ref ref="FILE" />
              <appender-ref ref="STDOUT" />
       </root>
</configuration>

if project is log intensive then custom filters can also be written and class of custom filter then can be added to logback.xml file.

Step 4: Logging
Logger logger = (Logger) LoggerFactory.getLogger(Sample.class);

Thats It!



No comments:

Post a Comment