Simple Logging Facade for Java (SLF4J)

Recently came across Simple Logging Facade for Java (SLF4J), it is easier to use as logging front end to Apache log4j than Apache Commons Logging (I use log4j as my logging system.)

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

Before you start using SLF4J, we highly recommend that you read the two-page SLF4J user manual.

In case you wish to migrate your Java source files to SLF4J, consider our migrator tool which can migrate your project into SLF4J in minutes.

In case an externally-maintained component you depend on uses a logging API other than SLF4J, such as commons logging, log4j or j.u.l, have a look at SLF4J’s binary-support for legacy APIs.

It did not take me too long to switch all my code over, a few hours and a couple of Perl scripts to do all the search-replace. The reason I switched is that it promises to have a much lighter CPU footprint compared to Commons Logging.
I did miss the Fatal logging level though:

Why doesn’t the org.slf4j.Logger interface have methods for the FATAL level?

From the stand point of a logging system, the distinction between a fatal error and an error is usually not very useful. Most programmers exit the application when a fatal error is encountered. However, a logging library cannot (and should not) decide on its own to terminate an application. The initiative to exit the application must be left to the developer.

Thus, the most the FATAL level can do is to highlight a given error as the cause for application to crash. However, errors are by definition exceptional events that merit attention. If a given situation causes errors to be logged, the causes should be attended to as soon as possible. However, if the “error” is actually a normal situation which cannot be prevented but merits being aware of, then it should be marked as WARN, not ERROR.

Assuming the ERROR level designates exceptional situations meriting close attention, we are inclined to believe that the FATAL level is superfluous.

I am not sure I buy their argument though, I had a number of fatal events logged in my code which would cause instant termination. Usually this because of missing configuration files, missing directories, failure to connect to a service to get vital information, stuff like that. I renamed them to errors and changed the message to be a little more urgent in tone.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: