39

So I just found out today that Log4J 2.0 is now actively being developed, there is an alpha version and it is said to replace logback.

Right now in my app I have close to 4 maybe more logging frameworks:

  • Java Util Logging
  • log4j
  • slf4j
  • logback (ignored thanks to a maven provided hack)
  • commons logging (ignored thanks to a maven provided hack)
  • And tomcat has its own JULI adapter

I have been using log4j (1.2.x) because frankly I just haven't needed the features of the newer guys but I have been tempted lately to switch to SLF4J and mainly because I don't want to have rewrite my complicated log4j configurations files to a new format (logback).

Now my question is in terms of what I should code against is SLF4J the right choice for the future given log4j 2.0.

It seems like I should just stick with old log4j (1.2.x) as it is the lowest common denominator?

UPDATE: on further examination of log4j 2.0 while very similar it appears the configuration is not backward compatible with log4j 1.2. Looks like logback is the best choice.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • For some projects we put together, logging framework was more driven by the open source frameworks we were using. But it is a state of preference. – sathish_at_madison Aug 23 '12 at 15:03
  • Yes I have done similar likewise but good ole log4j is almost in every project except for really really new projects and its the only one I can remember how to configure. The big annoyance in switching is actually the configuration and supported appenders. – Adam Gent Aug 23 '12 at 15:09
  • 7
    Code with SLF4J, and choose backend at runtime. Changing logging backend is not a problem if you are using SLF4J Api. – Toilal Aug 23 '12 at 15:29

2 Answers2

34

Disclaimer: I am the founder of log4j, slf4j and logback projects but unaffiliated with log4j 2.0.

As I understand it, notwithstanding its name, log4j 2.0 is very different than log4j 1.x. As far as the user API is concerned, log4j 2.0 is largely incompatible with log4j 1.x. Log4j 2.0 provides an adaptation layer for log4j 1.x which at present time (2012-08) is undocumented.

Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
Ceki
  • 26,753
  • 7
  • 62
  • 71
  • 4
    I guess I was hoping for a grand unification of logging (GUL) but I guess like physics (GUT) that isn't going to happen anytime soon :). BTW thanks for log4j (original). My interest in log4j 2.0 (over logback) is not having to switch configuration to the new logback style (IMHO its easier to refactor to SLF4J) than change the config of 10 or so projects. – Adam Gent Aug 23 '12 at 15:50
  • 2
    Are you familiar with the log4j.properties translator? It can greatly help in migrating log4j.properties to logback.xml. Try it out http://logback.qos.ch/translator/ – Ceki Aug 23 '12 at 15:56
  • 1
    I think I vaguely remember seeing that in the logback project but I need one for the XML format. – Adam Gent Aug 23 '12 at 15:57
  • I should enhance the translator to deal with log4j.xml as well. – Ceki Aug 23 '12 at 16:02
  • NM I'll file separate question and for now look at: http://mailman.qos.ch/pipermail/logback-user/2007-May/000209.html – Adam Gent Aug 23 '12 at 16:14
  • I could whip up some XSLT to translate log4j.xml to logback.xml. – pdxleif Jan 04 '13 at 19:03
  • 2
    The Log4J2 website reports that there is an adapter for SLF4J, so if you write your code to SLF4J you can switch to Log4J, Log4J2, and Logback whenever you want. Here's the bridge: http://logging.apache.org/log4j/2.x/log4j-slf4j-impl/ – Dave Sep 19 '13 at 03:14
16

It's a subjective question.

I'd suggest using slf4j, since it can use log4j as a backend if that's what you need.

You're likely to be using a number of components which may all use different logging APIs. It's good to be able to consolidate the output via those APIs into a single output route.

slim
  • 40,215
  • 13
  • 94
  • 127
  • 1
    Yes I think slf4j is what I'll go with. I got confused between facade and impl. Log4j 2.0 is not a facade and does not advertise to be one. – Adam Gent Aug 23 '12 at 15:06
  • I would also recommend to use [jcabi-log](http://www.jcabi.com/jcabi-log/) as a convenient static wrapper of slf4j – yegor256 Oct 05 '12 at 06:11