Logging in Java: Switching to logback and slf4j
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:
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.0</version> </dependency>
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); log.info("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:
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <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>The complete manual for logback is very detailed and available here.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Comments
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
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
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.