Difference in using java.util.logging and Log4j Loggers
Asked Answered
M

5

7

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.

Magnet answered 13/2, 2010 at 10:56 Comment(0)
C
10

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.

Carin answered 13/2, 2010 at 11:13 Comment(1)
Once going for SLF4J, then also go for the <a href="logback.qos.ch/">LogBack</a> as it is the improved version of log4j (same author).Piston
G
3

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.

Gamp answered 13/2, 2010 at 11:3 Comment(4)
Even for simple tasks, Log4j is much preferable to java.util.logging, which is just plain nastyWhisky
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.Gamp
Yes, well thankfully your brother's friend's homework assignments do not govern what is and isn't good practice in Java.Whisky
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.Gamp
S
3

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.

Schizophrenia answered 13/2, 2010 at 11:19 Comment(3)
Commons Logging used to have serious classpath loader problems. Be careful.Duad
Only in certain specific environments; in most situations it's fine.Whisky
Indeed you could see SLF4J as the improved version of commons-logging.Piston
M
3

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.

Motoneuron answered 13/2, 2010 at 11:25 Comment(0)
G
2

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

Genitalia answered 13/2, 2010 at 11:17 Comment(1)
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 (slf4j.org) and BackLog (logback.qos.ch).Piston

© 2022 - 2024 — McMap. All rights reserved.