tags:

views:

3034

answers:

7

Hi there,

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? Thanks for sharing your thoughts!

Okami

+17  A: 

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

For a good decision tree, have a look at http://java.sys-con.com/node/48541

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.

Matt Sheppard
+1  A: 

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

svrist
+2  A: 

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.

Edmund Tay
+13  A: 

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.

bmatthews68
+5  A: 

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!

Rydell
+3  A: 

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.

Will Sargent
+3  A: 

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

adam