jueves, abril 05, 2012

Hey! Teachers! Leave them kids alone! All in all it's just another brick in the wall. All in all you're just another brick in the wall. (Another Brick In The Wall - Pink Floyd)


In current post I am going to show you how to configure your application to use slf4j and logback as logger solution.

The Simple Logging Facade For Java (slf4j) is a simple facade for various logging frameworks, like JDK logging (java.util.logging), log4j, or logback. Even it contains a binding tat will delegate all logger operations to another well known logging facade called jakarta commons logging (JCL).

Logback is the successor of log4j logger API, in fact both projects have the same father, but logback offers some advantages over log4j, like better performance and less memory consumption, automatic reloading of configuration files, or filter capabilities, to cite a few features.

Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.

First we are going to add slf4j and logback into pom as dependencies

Note that three files are required, one for slf4j, and two for logback. The last two dependencies will change depending on you logging framework, if for example you want to still use log4j, instead of having logback dependencies we would have log4j dependency itself and slf4j-log4j12.

Next step is creating the configuration file. Logback supports two formats of configurations files, the traditional way, using XML or using a Groovy DSL style. Let's start with traditional way, and we are going to create a file called logback.xml into classpath. File name is mandatory, but logback-test.xml is also valid. In case that both files are found in classpath the one ended with -test, will be used.

In general file is quite intuitive, we are defining the appender (the output of log messages), in this case to console, a pattern, and finally root level logger (DEBUG) and a different level logger (INFO) for classes present in foo package. 

Obviously this format is much readable than typical log4j.properties. Recall on additivity attribute, the appender named STDOUT is attached to two loggers, to root and to com.lordofthejars.foo. because the root logger is the ancestor of all loggers, logging request made by com.lordofthejars.foo logger will be output twice. To avoid this behavior you can set additivity attribute to false, and message will be printed only once.

Now let's create to classes which will use slf4j. First class called BarComponent is created on com.lordofthejars.bar:


Note two big differences from log4j. The first one is that is no longer required the typical if construction above each log call.  The other one is a pair of '{}'. Only after evaluating whether to log or not, logback will format the message replacing '{}' with the given string value.

The other one called FooComponent is created on com.lordofthejars.foo:

And now calling foo and bar method, with previous configuration, the output produced will be:

Notice that debug lines in foo method are not shown. This is ok, because we have set to be in this way. 

Next step we are going to take is configuring logback, but instead of using xml approach we are going to use groovy DSL approach. Logback will give preference to groovy configuration over xml configuration, so keep in mind it if you are mixing configuration approaches.

So first thing to do is add groovy as dependency.

And then we are going to create the same configuration created previously but in groovy format.

You can identify the same parameters of xml approach but as groovy functions.

I wish you have found this post useful, and in next project, if you can, use slf4j in conjunction with logback, your application will run faster than logging with log4j.

Keep Learning,
Alex.


4 comentarios:

Anónimo dijo...

In submodels I can declare only the slf4j-api dependency?
Thanks

Anónimo dijo...

It's not a matter of sub-module or not. You should only include a concrete implementation (logback, log4j, whatever) in "runnable" artefacts, such as WAR, EAR or executable JARs. Everywhere else, your sole logging dependency should be the slf4j-api.

(In fact, you may want to include a log implementation for testing, i.e. with "test" scope. I tend to use slf4j-simple for that, as it requires no configuration and is good enough for the purpose.)

Anónimo dijo...

Some comments on the original post:

- It's enough to include logback-classic as a dependency (it'll pull in -core). But you should really include it with scope "runtime". You don't want to have dependencies on logback sneaking into your code!

- The mentioned "if(isDebugEnabled" construct is ridiculously overused by some Java developers. In most cases it's not worth doing that (especially if the log string isn't even concatenated!) and a classic example of premature optimization that clutters the code. It may be worth doing it for TRACE and log statements that are built using a loop, but then even slf4j's parameterised logging won't help you.

- It's quite unlikely that using logback will give you any noticeable performance advantage in your application over log4j. If it does, you're probably logging way too much ;-) I also consider logback superior, but not for performance reasons.

Alex Soto dijo...

Thank you very much all of you for reading post, about last comment:

- yes you are right runtime scope is better.
- it just what I have said, there is no need to use typical isDebugEnable form.
- about performance, I am partially agree with you, it is true that if you log too much, using slf4j or log4j, will be bad, and no noticeable performance will be seen, but according to logback documentation, logback runs faster in some situations than log4j.