Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare log4j and Logger

How does The JDK's Logger compare to Apache log4j?

Which one is better for new projects that target Java 6? How do they compare in terms of flexibility and configurability?

like image 412
notnoop Avatar asked Sep 03 '09 20:09

notnoop


People also ask

Is Logger part of Log4j?

Interface Logger. This is the central interface in the log4j package. Most logging operations, except configuration, are done through this interface. The canonical way to obtain a Logger for a class is through LogManager.

Is Log4j and Apache Log4j different?

Log4j is part of the Apache Logging Services Project -- an open source effort within the Apache Software Foundation. The Apache Logging Services Project includes multiple variations of the Log4j logging framework for different programming deployments and use cases.

What is difference between Log4j and SLF4J?

Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.

What is the difference between Log4j and Log4j2?

Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.


2 Answers

To my mind the only thing the JDK Logger has going for it is that it is part of the JDK, so it doesn't add an external dependency. If the choice is only between those two, I'd go with Log4j. It still has better support in terms of appenders, the number of people who know it (in my anecdotal observations), and a better API (that is subjective as well).

Starting a project today, the most tempting thing to do is go with slf4j and deffer the decision - you can always plug in a different framework underneath slf4j by just changing the classpath.

That being said there are other options (such as Log5j) that take advantage of the latest Java language features. I'd recommend taking a long look Logback (from one of the main programmers of Log4j, as is slf4j).

like image 140
Yishai Avatar answered Sep 23 '22 10:09

Yishai


I've never used the direct JDK logger, but for a new project I'd highly reccomend Logback, designed as a successor for log4j. Some of the very nice things you can do with it:

  • printf style parameter building, no more messy concatenating strings protected by if logger.isDebugEnabled() guards.

go from

if (log.isDebugEnabled())
 { 
    log.warn (i + "many ints,"+ l+"many longs");
}

to

log.debug("{} many ints, {} many longs", i, l);
  • very flexible config, including configurations that will print traces. The config is xml, but their site includes a utility that will generate an xml config from your log4j config to get you started.

Downside - lots of packages require log4j anyway, since it's so common, so your project may need to include 2 logging packages.

like image 24
Steve B. Avatar answered Sep 24 '22 10:09

Steve B.