6

I have read similar questions on SO like this and this. But they are about four years old!

Also I have read this logback page, which has some really good info on why to choose Logback over log4j.

I am looking to implement a logging framework for a project with the following technology stack -

  • Spring
  • Hibernate
  • Maven
  • Tomcat
  • Rest

I have already decided to use slf4j as the facade, so this question is on whether to use slf4j + log4j or slf4j + logback (I know that logback natively uses slf4j).

What I am looking for is following -

  • Has anyone had an experience with Logback that would prove it to be not as mature or efficient as log4j?
  • How does it fair as compared to log4j in a multi-threaded environment?
  • Ability to replace tomcat-default jul logging with logback/log4j logging
  • Ability to consolidate logging configuration into a common file for a maven multi-module project
  • Logback claims it's 10 times faster than log4j, has anyone validated that claim? (as part of my research I do plan to run some tests to measure performance and will post back my results)

EDIT: I've read at many places (one of the answer below states this as well) that log4j is dead/deprecated. Contrary to that, log4j just released an alpha version of its 2.0 release. So I do not buy that argument.

Community
  • 1
  • 1
gresdiplitude
  • 1,665
  • 1
  • 15
  • 27
  • Related (and recent), if not exact duplicate: http://stackoverflow.com/questions/11359187/dont-get-it-why-not-jul – assylias Jul 17 '12 at 09:54
  • 3
    Log4j2 has nothing to do with log4j as Struts 1 has anything to do with Struts 2 which is based on a code base donated from Webworks. Log4j2 is not backwards compatible with log4j. And lastly, Log4j2 is still in alpha, we're approaching Q3 of 2012. Log4j2 != log4j. Log4j roadmap is dead. – Oh Chin Boon Jul 18 '12 at 10:57

1 Answers1

11

LogBack successes Log4J, it is from the author who made Log4J. Log4J is currently deprecated.

Here is an example of where SLF4J / LogBack primes:

Log4J (Current)

Object entry = new SomeObject();
logger.debug("The new entry is "+entry+".");

Here are the tasks performed:

  1. Java calls on toString method on the variable entry to get the String literal representation. Java initializes space in heap to hold the String object.
  2. Java creates a new String based on the above String, with “The new entry is “ and “.”. Java initializes space to hold the yet newer String literal.
  3. Java passes on the newly constructed String object as input parameter to Log4J. Log4J checks if the current logging level is DEBUG and above. (assuming it was set to INFO in logging.xml descriptor)
  4. Log4J does not print out the String literal into the log, since the logging level is set to INFO. (Therefore all the work on 1 – 2 was wasted)

SLF4J / LogBack (New)

Object entry = new SomeObject();
logger.debug("The new entry is {}.", entry);

Here are the tasks performed:

  1. Java passes on the String object “The new entry is {}.” and object entry as input parameters to SLF4J. SLF4J checks if the current logging level is DEBUG and above. (assuming it was set to INFO in logging.xml descriptor)
  2. Log4J does not print out the String literal into the log, since the logging level is set to INFO.

http://www.slf4j.org/faq.html#logging_performance

Ability to replace tomcat-default jul logging with logback/log4j loggin?

Yes, use with SLF4J.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • 1
    I can still achieve this if I use slf4j + log4j. Why logback can accomplish this is because it natively uses slf4j, so thats +1 in favour of slf4j, which I anyway DO plan to use irrespective of the decision to go with log4j or logback. – gresdiplitude Jul 17 '12 at 10:03
  • 2
    Also log4j just released an [alpha version](http://logging.apache.org/log4j/2.0/index.html) of its 2.0 release on 2012-06-09, so I doubt its deprecated. – gresdiplitude Jul 17 '12 at 10:17
  • 1
    Definitely not deprecated, but logback is generally an improvement, is a stable, mature lib, meshes better with SLF4J (which means no crazy classloader magic), so there's little reason to start a new project with log4j. – Marko Topolnik Jul 17 '12 at 11:00
  • 1
    There is little going on for 2.0 @Vaishali – Oh Chin Boon Jul 17 '12 at 12:29
  • @Chin Boon Checkout their website, looks like log4j is trying to fight-back with Logback :) `Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback's architecture.` – gresdiplitude Jul 17 '12 at 13:35
  • "Log4j 2 is now available for testing." Go to the Downloads page here, http://logging.apache.org/log4j/2.x/download.html It is still in alpha stage.. How long are you going to wait more? :) Logback / SLF4J was production ready ages ago. – Oh Chin Boon Jul 18 '12 at 09:31
  • 2
    For the record, Log4J development is deprecated. Log4j2 has nothing to do with Log4j as Struts 1 has anything to do with Struts 2 in the context that Struts2 is based off WebWorks, which is a code base donated to Struts. Log4j2 is not backwards compatible with Log4j btw. – Oh Chin Boon Jul 18 '12 at 10:50
  • 1
    The log4j 2 alpha-1 release notes further detail the architectural problems they see in competing loggers: http://comments.gmane.org/gmane.comp.jakarta.log4j.user/19743 – Stefan L Apr 25 '13 at 10:45