Thursday, August 8, 2013

Pursuit of Technical Freedom: Configure Logback for Web Applications

Pursuit of Technical Freedom: Configure Logback for Web Applications: This is step by step guide to configure Logback as logging framework for your application Step 1: Adding Dependencies        &l...

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!



Steps to Setup Playframework Build Jobs using Jenkins

Steps to Setup Playframework Build Jobs using Jenkins

1. Download playframework 2.x version, unzip and place it some directory:

               Download path: http://www.playframework.com/download

2. Add play path to system variable e.g C:/play

3. Download Jenkins:

              Download path: http://jenkins-ci.org/node/296

4. After installation is complete, go to manage Jenkins:





5. Download play-auto-test plugin:

             Download path: http://updates.jenkins-ci.org/download/plugins/play-autotest-plugin/

6. Go to Manage plugins, and install downloaded plug in
7. Open configure System, select JDK and give JAVA_HOME path:


8. Give path to play framework script.


9. Go to New Job, give build job name, select “Build a free-style software project”

10. Press OK, in next screen select source code management system and give path to repository:

11. In “Build”, select “Play” and give play commands, in post build actions, select play! Auto-test reports:

12. Save it, build job is ready.