Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Logback mature enough to replace log4j? [closed]

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.

like image 617
gresdiplitude Avatar asked Jul 17 '12 09:07

gresdiplitude


People also ask

Is Logback better than log4j?

Key Difference Between Log4j vs LogbackAs logback is improved, version log4j and versions log4j2 and logback have no difference in terms of performance or any features. Therefore log4j is the most used logging utility before the logback newer versions were invented.

Is log4j alternative to Logback?

Logback is one of the most widely used logging frameworks in the Java Community. It's a replacement for its predecessor, Log4j. Logback offers a faster implementation, provides more options for configuration, and more flexibility in archiving old log files.

Can I use both log4j and Logback?

As the slf4j documentation says, you just have to replace all the log4j dependencies with a single one from slf4j, named log4j-over-slf4j: http://slf4j.org/legacy.html#log4j-over-slf4j. Any code that is under your direct control can just use slf4j + logback as it always would.


1 Answers

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.

like image 190
Oh Chin Boon Avatar answered Sep 24 '22 01:09

Oh Chin Boon