Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Logger.getLogger(className) and LogFactory.getLog(className )?

I know its a package difference

1) org.apache.log4j.Logger logger = Logger.getLogger(clazz);

2) org.apache.commons.logging.Log log = LogFactory.getLog(clazz);

The first one uses loggers via log4j and the second one uses commons.logging. We have a huge project where in some classes loggers are configured using log4j and in some cases its commons.logging.

I did find a log4j property file though.Is there a similar property file for commons.logging ? Where do I configure for commons-logging ?. I am unable to see the logs generated by commons-logging.

Any help is appreciated.

like image 772
Shashank Kadne Avatar asked Apr 20 '12 12:04

Shashank Kadne


People also ask

What is logger getLogger?

The getLogger() method of a Logger class used find or create a logger. If there is a logger exists with the passed name then the method will return that logger else method will create a new logger with that name and return it. There are two types of getLogger() method depending upon no of the parameter passed.

What is the difference between log and logger in java?

The call to LogFactory. getLog() is from the commons-logging api. log4j is a logging framework, i.e. it provides the code to log messages. Commons-logging is an abstraction layer for logging frameworks, it doesn't log anything itself.

What is LogManager getLogger?

public class LogManager extends Object. The anchor point for the Log4j logging system. The most common usage of this class is to obtain a named Logger . The method getLogger() is provided as the most convenient way to obtain a named Logger based on the calling class name.

What is Logger class in java?

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.


1 Answers

Yes, commons-logging is a facade API that was suppose to abstract you from underlying logging framework (in practice there was a choice between log4j and java.util.logging) so that you could switch from one to another without touching the code - just by switching libraries available on the CLASSPATH.

Unfortunately due to some design mistakes it had issues with complex class-loading environments, like application servers. Currently it is effectively superseded by slf4j.

In your case I would recommend sticking with one API - either Log4J or commons-logging, even though commons-logging will (most likely) delegate to log4J. You can also migrate to using SLF4J and install bridging APIs, but this is slightly more advanced.

like image 69
Tomasz Nurkiewicz Avatar answered Sep 19 '22 12:09

Tomasz Nurkiewicz