Performance Zone is brought to you in partnership with:

Graduated in computer science at the University of Oldenburg, Germany. Develops software for over 20 years now. Likes to program in Java. Wrote and contributed Maven plugins and implemented initially Maven support for IntelliJ IDEA. Ralf is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

Logging in Java: Switching to logback and slf4j

  • submit to reddit

 log4j was and maybe still is the de facto standard when it comes to logging in Java applications.
Sun's solution with the internal JDK logging could not be enforced across the board. The reasons for this are certainly the lack of configurability and flexibility. For simple projects the JDK logging is certainly a solution, but not for enterprise applications.

Now, in addition to log4j a new implementation that is more powerful, faster and more flexible than log4j has entered the market: logback. Ok, in fact logback was started in 2006, but version 1.0 was released in Nov. 2011.

logback has been created as a successor to log4j by the same developer and is now available after many years of testing and development in a version 1.0 (current version is 1.0.1). To avoid misunderstandings due to the small version number it should be said that logback is already in use for years in business and the version number does not reflect in any case a statement about the stability or functionality.

logback provides several advantages over log4j. Among other things:

  •      Much faster implementation
  •      Automatic reloading of logging configuration
  •      Better filter
  •      Automatic compression of archived log files
  •      Stack traces with information about the manufacturing Java Package (jar file)
  •      Automatic removal of old log archives

For the developer a switch from log4j to logback is very easy. Just switch a dependency in your Maven POM and you are ready to go:
Thanks to the transitive dependencies you now also have the logging facade slf4j added to your project.
A "Hello World" example using slf4j looks like this:
 package demo;  
 import org.slf4j.Logger;  
 import org.slf4j.LoggerFactory;  
 public class HelloWorld {  
   public static void main(String[] args) {  
    Logger log = LoggerFactory.getLogger(HelloWorld.class);"Hello World");  
All that remains is a configuration file to control log output. 

With log4j it is usually called log4j.xml. With logback it is called logback.xml or logback-test.xml for testing environment.

In Maven projects the file logback.xml must be placed into $PROJECT_HOME/src/main/resources. The file logback-test.xml must be placed into $PROJECT_HOME/src/test/resources. A simple configuration looks like this:
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
   <!-- encoders are assigned the type  
      ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->  
    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>  
  <root level="debug">  
   <appender-ref ref="STDOUT" />  
The complete manual for logback is very detailed and available here.

Published at DZone with permission of Ralf Quebbemann, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Fabrizio Giudici replied on Sun, 2013/02/17 - 6:39am

Definitely +1 for slf4j and logback. Most of the recent FLOSS products standardize on slf4j, I've been using both in production, even for my customers, and it's fine.

Grzegorz Grzybek replied on Mon, 2013/02/18 - 2:18am

First I've been using commons-logging but it was pain in the long term. I've made a switch to SLF4J+Logback when I saw Hibernate started using SLF4J. But the've made a switch to JBoss Logging which (for me) is strange... regards Grzegorz Grzybek

Ralf Quebbemann replied on Mon, 2013/02/18 - 3:20am in response to: Grzegorz Grzybek

JBoss Logging is also new to me. Have to check it out. Maybe the reason for switching to JBoss Logging is a better integration into the JBoss stack, or not relying too much on external libraries.

Grzegorz Grzybek replied on Mon, 2013/02/18 - 4:08am in response to: Ralf Quebbemann

JBoss logging is as external as slf4j :) I've read their explanation (e.g. I18N for logging messages). What's strange for me is that reference JPA implementation with the goals to be a model realization of "vanilla JavaEE" programming model depends on particular application server (at least on its name :))...

Franz Van Betteraey replied on Wed, 2013/02/20 - 9:11am

If you speak of log4j you probably mean the log4j 1  project. This is indeed outdated.

But your log4j link targets the project site of the new log4j 2  project. The last beta-4 version was released in january 2013  - thus it is under active development. So you should be fair to those who are working on the log4j 2 project and clarify the situation.

By the way - I am not involved in any logging projects ;-)

Ralf Quebbemann replied on Wed, 2013/02/20 - 10:17am in response to: Franz Van Betteraey

As of the time I originally wrote the article  (April 2012) there were no proceedings on the log4j project for the 2.x version. The docs on their website (and the URL I used in my article) were referencing the 1.2.x version.

Of course now the situation is different. The Apache folks have continued to develop log4j 2.x and I welcome that. It's always good to have the choice and to use libs which are under active development.

Franz Van Betteraey replied on Thu, 2013/02/21 - 2:20am in response to: Ralf Quebbemann

Oh, I see. I am not familiar with the "republishing habits" of DZone but IMHO it is not a good idea to publish an old (but interesting) article with a new date. It should be obvious when the article was written. Nevertheless thanks for posting.

Michael Je replied on Fri, 2013/04/05 - 7:50am

 JBoss Logging is also new to me. Have to check it out. Maybe the reason for switching to JBoss Logging is a better integration into the JBoss stack, or not relying too much on external libraries.

Post Here

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.