Logging in java Part-1

Logging is one of the most important part of an application development. A well logging enabled application not only keeps track of the application flow but it also helps in debugging. As application grows logging even becomes more important because it helps in finding a bug which seldom comes on production. There may be thousands of logging requests generated per second depending upon the application size in that case logging should not hamper application performance. Apart from performance there are other things like configuration, abstraction, encoders, appenders and filters which should be taken into consideration while choosing a logging framework. 
In java there are 5 commonly used logging frameworks:
  1. Java logging framework (JUL)
  2. Jakarta Commons logging (JCL)
  3. Simple logging facade for java (SLF4J)
  4. Log4j
  5. Logback
Log4j is most widely used logging framework in conjunction with JCL or SLF4j. In this article I'll start with basic comparasion between log4j and logback later on I'll go in detail about how to configure logback in your project. JCL and slf4j are abstract logging framework mostly used by library developers while other 3 libraries do actual logging. Earlier jcl was used by most of the libraries but since it has runtime class loading problems which often result in annoying exceptions so you will see library developers are upgrading to slf4j based logging which not only overcomes jcl class loading problem but it also provides other useful functionality like support for arguments in logging statement.  Slf4j can be plugged with most of the logging frameworks like jcl, log4j, logback and jul.

Log4j File Appender: Following code logs in a file test-log4j.log as well as console.

   
      
         
      
   
   
      
      
      
      
         
      
   
   
      
      
      
   

Logback File Appender: Following code logs in a file test-logback.log as well as console.

   
     
       %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
     
   
   
      /home/mukesh/logs/testing/test-logback.log
      
         %date %level [%thread] %logger{10} %msg%n
      
   
   
      
      
   


Logback vs Log4j File Appender Performance Graph

Above graph represents the performance of logback and log4j for file appender using single logging statement. Y-axis represents number of logging statements and X-axis represents number of threads. Both programs were run for same time for each thread set on the same machine. In this graph clearly logback has an advantage because it takes less time as compared to log4j in logging a statement.

Log4j JDBC Appender: Following code logs in a mysql table log4jTable as well as console.

   
      
         
      
   
   
      
      
      
      
      
   
   
      
      
      
   


Logback JDBC Appender: Following code logs in a mysql table logging_event as well as console.

   
      
         %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
      
   
   
      
         
            com.mysql.jdbc.Driver
            jdbc:mysql://localhost:3306/logback
            root
            
         
      
      
   
   
      
      
   


Logback vs Log4j JDBC Appender Performance Graph

Above graph represents the performance of logback and log4j for jdbc appender using single logging statement. Y-axis represents number of logging statements and X-axis represents number of threads. Both programs were run for same time for each thread set on the same machine. In this graph clearly performace of logback is miles ahead of log4j primarily because logback uses threadpooling and it has standard tables for logging, script is packaged with logback distribution. There is no out of box configuration for thread pool in log4j.

In the next part I'll discuss about how to configure slf4j and logback in a project. Stay tuned ...
Read More