tags:

views:

300

answers:

5

Hello friends, I am developing a java application for which i have to use a logging mechanism. And now i am confused to choose either java libraries logger or to go for Log4j logger.

So i want to know when i can go for java logger and when i can go for log4j logger.

+2  A: 

Logger class was not part of jdk earlier on, so several library implementations sprung up. The Log4j library has one of the most comprehensive set of logging utilities (Formatters, Appenders etc). However, for most developers this would be an overkill and the simple java.util.Logger would suffice.

I personally use a custom wrapper over my logger implementation. This enables me to define custom calls to carry out functional logging/auditing.

questzen
Even for simple tasks, Log4j is much preferable to `java.util.logging`, which is just plain nasty
skaffman
Probably not! I remember one of my younger brother's friend sending his assignment which would not run because of log4j dependency. The problem is with classpath, his reviewer was not so impressed. Some people are just reluctant to learn.
questzen
Yes, well thankfully your brother's friend's homework assignments do not govern what is and isn't good practice in Java.
skaffman
Neither do I wish so, the point I was making was that the dependency on external jar files can wreck havoc and can be avoided.
questzen
+7  A: 

I'd suggest you go with SLF4J instead to decouple your application from specific logging frameworks. It has adapters for various popular logging frameworks such as Jakarta Logging, JDK1.4 logging, log4j etc. making it a good abstraction for logging needs.

Esko
Once going for SLF4J, then also go for the <a href="http://logback.qos.ch/">LogBack</a> as it is the improved version of log4j (same author).
Verhagen
+1  A: 

I find Log4j more flexible when it comes to tweaking the logging cfg without re-compiling code in production environment.

daedlus
Yes, I would always choose / advise to use log4j, over default Java logging. Then I discovered commons-logging, where it is possible on start-up to make that decision. Which is great, as maybe in production standard Java is required (for whatever reason). And during development you have the flexibility / power of log4j. But nowadays I would no longer use both of these packages! The packages I would advise to use now are SLF4J (http://www.slf4j.org/) and BackLog (http://logback.qos.ch/).
Verhagen
+3  A: 

There are the Apache Commoms Logging project and SLF4J, either of which abstracts the underlying logging library.

In practice I tend to use Log4J over the built in logging classes. Mainly because Log4J can be configured per web-app in an application server, whereas JDK logging is configured per JVM.

Nate
Commons Logging used to have serious classpath loader problems. Be careful.
Thorbjørn Ravn Andersen
Only in certain specific environments; in most situations it's fine.
skaffman
Indeed you could see SLF4J as the improved version of commons-logging.
Verhagen
+3  A: 

The approach I would currently recommend is to use SLF4J as the logging API. You can then pick your logging framework depending on your needs as you discover them.

I did a writeup on what I consider to be best practice in getting started with SLF4J and a simple "log to System.out" which is currently placed at. http://runjva.appspot.com/logging101/index.html

Hopefully it is helpful.

Thorbjørn Ravn Andersen