7 Feb 2008

Comparison: java.util.logging vs org.apache.commons.logging

Here's a comparison between the java logging system and apache commons logging system.

For the lazy ones here's the conclusion:
  • Commons Logging Rocks!
  • Java Logging Sucks!

Why ? Well because:
  • Commons Logging api is better
  • Commons Logging let you choose your logging implementation
  • Commons Logging is bulletproof, production ready, heavily use everywhere
  • Everybody knows Commons Logging

Not convince yet ? Good I like people that don't take things for granted easily ;)

Let's compare the 2 frameworks api:
Java LoggingCommons Logging
Unclear log level: SEVERE > WARNING > INFO > CONFIG (wtf?) > FINE > FINER > FINEST (missing some imagination here ?!)Clear log level using different names: FATAL > ERROR > WARN > INFO > DEBUG > TRACE
No ERROR level! I find it really disturbing especialy since there is a System.err stream for so many yearsThere is an ERROR level
To log an exception:
logger.log(Level.SEVERE, e.getMessage(), e);
or may be
logger.log(Level.WARNING, e.getMessage(), e);.
Unclear what Level to use and why do I need to do e.getMessage() it could have been done in the log() method.
Missing simple methods: logger.warning(Throwable t) and logger.severe(Throwable t)
To log an exception: logger.error(e);
To configure either modify the logging.properties in your JRE/lib folder or set -Djava.util.logging.config.file=/my/folder/logging.properties
No way to put a conf in the classpath to be automaticaly pick up by the logging framework
Possibility to put an already configured conf file
Create a logger:
private static Logger logger = Logger.getLogger(MyCLass.class.getName());
Missing method: Logger.getLogger(class)
Create a logger:
private Logger logger = Logger.getLogger(getClass());


The good things about Java Logging:
  • Classloader: About the configuration part the way java logging does the configuration is less flexible but might reduce classloader problems that you encounter with commons logging (see: Commons_Logging_FUD)

  • No jars to add, No additional dependency


Working with Commons Logging api is some much better, simple and easier that I will continue to put those 2 jars in the classpath whenever possible if that's what it takes to use it.

Now try both and make your own opinion ;)

8 comments:

Nicolas said...

And what about Lucene logging system:
http://lucene.apache.org/java/2_3_0/api/core/org/apache/lucene/index/IndexWriter.html#setInfoStream(java.io.PrintStream)

:D

kartben said...

Great comparative study ! :-)

dkowis said...

This is so not a good comparison. For one the java.util.logging stuff is wrong. Things are much simpler.

Logger.finest("string");
logger.log(Level.WARNING, "message", exception);

it does smart things...

This is old, and biased...

Anonymous said...

Indeed, this is a dummy comparison :

- SEVERE is more adapted for a Level than Error, this is not a classification of what is a warning, an information or something. Only Config is weird and has been added because it is handy.
- Logging.severe(e); exists
- You are comparing a static vs non-static logger. Here's a non-static with standard logging : Logger.getLogger(getClass().getName()). And this is a logger name, you win ".getName()" for each instance: wow!
- you can use a custom class to load conf, a file on-disk, load it from the classloader is so easy with LogManager.readConfiguration, ... you can load it from wherether you want. The standard API is better.

And mainly, the JDK logger has a better API for handling formatting details with Object[]. I only created an intermediate class that wraps those in varargs (...) to be even nicer to have.

Well, your post is really wrong, we just see you're almost angry about it.

A really, nicer, better alternative is SLF4J ...

Benjamin Francisoud said...
This comment has been removed by the author.
Benjamin Francisoud said...
This comment has been removed by the author.
Benjamin Francisoud said...

> Indeed, this is a dummy comparison

well it's your opinion, I took 2 hours to test and write this article, with 8 years of using logs including log4j, I don't consider this dummy...

And I take the time to answer you, which hopefully will prove I'm not just doing FUD.

- SEVERE is more adapted for a Level than Error, this is not a classification of what is a warning, an information or something.

SEVERE equivalent is FATAL in log4j, I don't care they are just synonyms.
I use this level for http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/lang/Error.html that means I almost never use them.
I suppose you meant you don't like 'Error' in log4j, I find it clear.
I use it for http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html, they could have call it Exception which would have been even better.
but java logging just gives WARNING, an exception is not a warning in my world. That means I'm force to use SEVERE and that leave me noting to log java.lang.Error, the reason I don't like java logging.

> Only Config is weird and has been added because it is handy.

Usually you turn lower log level to a lower level to debug, when I will reach config I will be fill with 'config' informations I just dont want usually I find them in info level and that's just fine.


> - Logging.severe(e); exists

Logging ? did you mean Logger ?
I donc see any Logging object in logging package ans Logger don't take a thrwoable just a string:
http://download-llnw.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#severe%28java.lang.String%29

> - You are comparing a static vs
> non-static logger. Here's a
> non-static with standard logging :
> Logger.getLogger(getClass().getName()).
> And this is a logger name, you win
> ".getName()" for each instance: wow!

I like elegant code, if you can avoid unnecessary boilerplate code then the api should provide it.

> - you can use a custom class to load
> conf, a file on-disk, load it from
> the classloader is so easy with
> LogManager.readConfiguration, ... you
> can load it from wherether you want.

I doubt log4j can't do that already

> The standard API is better.

It's your opinion, not mine.

> Well, your post is really wrong, we
> just see you're almost angry about it.

No, not angry at all, I decided to not use it long time ago and I didn't change my mind.

Funny to see a writing : "sucks" just create such reactions :)

> A really, nicer, better alternative
> is SLF4J ...

Yes it's nicer even better than log4j.
Slowly replacing log4j.
[troll]Notice how the java logging api did'nt replace it ;)[/troll]

Nico said...

What a bad and simple comparative ! It's not java logging which sucks... but your comparative...