Java Logging vs Log4J [closed]
Asked Answered
R

7

151

Is it still worth to add the log4j library to a Java 5 project just to log let's say some exceptions to a file with some nice rollover settings. Or will the standard util.logging facility do the job as well?

What do you think?

Roundy answered 28/8, 2008 at 8:20 Comment(1)
See https://mcmap.net/q/160257/-production-settings-file-for-log4j for numerous log4j 1.2 locking issues to be considered.Noctilucent
S
127

I'd say you're probably fine with util.logging for the needs you describe.

For a good decision tree, have a look at Log4j vs java.util.logging

Question One : Do you anticipate a need for any of the clever handlers that Log4j has that JUL does not have, such as the SMTPHandler, NTEventLogHandler, or any of the very convenient FileHandlers?

Question Two : Do you see yourself wanting to frequently switch the format of your logging output? Will you need an easy, flexible way to do so? In other words, do you need Log4j's PatternLayout?

Question Three : Do you anticipate a definite need for the ability to change complex logging configurations in your applications, after they are compiled and deployed in a production environment? Does your configuration sound something like, "Severe messages from this class get sent via e-mail to the support guy; severe messages from a subset of classes get logged to a syslog deamon on our server; warning messages from another subset of classes get logged to a file on network drive A; and then all messages from everywhere get logged to a file on network drive B"? And do you see yourself tweaking it every couple of days?

If you can answer yes to any of the above questions, go with Log4j. If you answer a definite no to all of them, JUL will be more than adequate and it's conveniently already included in the SDK.

That said, pretty much every project these days seems to wind up including log4j, if only because some other library uses it.

Sacral answered 28/8, 2008 at 8:37 Comment(3)
Great questionnaire based answer. To each his own based on needs.Ernestinaernestine
Log4j vs java.util.logging link doesn't work anymorePages
Cheers - Changed it out for a wayback machine one.Sacral
Q
45

I recommend that you use the Simple Logging Facade for Java (SLF4J). It supports different providers that include Log4J and can be used as a replacement for Apache Commons Logging.

Quiff answered 28/8, 2008 at 10:58 Comment(4)
What's wrong with Commons Logging?Weever
@Bart van Heukelom and comment upvoters - read articles.qos.ch/thinkAgain.htmlVizierate
@Stephen C: Thanks for the info, though I learned that some time ago and am now using SLF4J whenever I can. (My comment was a real question btw, not a conservative remark)Weever
@Bart, Commons Logging use dynamic discovery of the package to use. SLF4J use (or used to) need the classes in the backend to load at all.Angel
L
23

Log4j has been around for a long time, and it works very well. I have no scientific study to back it, but based on what I've seen at a large number of clients, it is easily the logging framework that I see used more than any other. It has been around for a long time, and not been replaced by the Next Big Logging Framework, which says something.

It is dead simple to set up, and easy to learn the basic appenders (outputs). There are a whole host appenders that are available, including:

  1. ConsoleAppender
  2. DailyRollingFileAppender
  3. ExternallyRolledFileAppender
  4. FileAppender
  5. JDBCAppender
  6. JMSAppender
  7. NTEventLogAppender
  8. RollingFileAppender
  9. SMTPAppender
  10. SocketAppender
  11. SyslogAppender
  12. TelnetAppender
  13. WriterAppender

Plus others. It isn't difficult to write your own appender either. Additionally there is a great deal of flexibility in each of the appenders that allow you to control specifically what is output in your log.

One note, I had a series of classloader problems when I used apache commons logging in addition to log4j. It was only for one specific application, but I found it simpler to use log4j alone, rather than to have the flexibility offered when using an abstraction layer like commons logging.

See this article for more details:

Good luck!

Lauricelaurie answered 30/8, 2008 at 23:34 Comment(0)
V
18

java.util.logging offers a comprehensive logging package without the excess baggage some of the others provide..

Vocalic answered 16/12, 2008 at 10:55 Comment(1)
Plus, it is included by default and used by Java itself (so you will have it anyway!)Sobriety
N
7

log4j is a much nicer package overall, and doesn't have some of the hiccups that java.util.logging contains. I'd second that using log4j directly is easier than using the commons logging.

Nebo answered 9/9, 2008 at 3:57 Comment(2)
Can you please be more specific about the hiccups that java.util.logging has ?Lycanthropy
Sure, like the overhead in logging synchronized methods: see github.com/playframework/playframework/issues/6958 and bugs.openjdk.java.net/browse/JDK-8077846Nebo
L
4

I recommend using Apache Commmons Logging as your logging interface. That way you have the flexibility to switch logging implementations anytime you want without requiring any code changes on your end.

Liege answered 28/8, 2008 at 9:40 Comment(0)
M
3

I would go with log4j. The possibilites with log4j is not obsolete at all!

Mcdaniel answered 28/8, 2008 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.