Pawan
Pawan

Reputation: 32321

relation between log4j and apache.commons.logging

Could anybody please tell me what's the relation between log4j and apache.commons.logging ? which one is more popular and better ?

I have imported an existing Application in which they have imported import org.apache.commons.logging.LogFactory; and written a separate class called Log and overridden some of the methods as critical, warn, info

For instance

 public static void info(String caller, String toWrite)
   {
      write(caller,toWrite,Log.INFO);
   }

Could anybody please tell me is log4j and apache.commons.logging are different ??

Upvotes: 17

Views: 22838

Answers (3)

Vasanth Saminathan
Vasanth Saminathan

Reputation: 585

As per java-doc documentation of commons-logging

Concrete subclass of LogFactory that implements the following algorithm to dynamically select a logging implementation class to instantiate a wrapper for:

  • Use a factory configuration attribute named org.apache.commons.logging.Log to identify the requested implementation class.
  • Use the org.apache.commons.logging.Log system property to identify the requested implementation class.
  • If Log4J is available, return an instance of org.apache.commons.logging.impl.Log4JLogger.
  • If JDK 1.4 or later is available, return an instance of org.apache.commons.logging.impl.Jdk14Logger.
  • Otherwise, return an instance of org.apache.commons.logging.impl.SimpleLog

Hence inference is:

  • commons-logging is more of abstraction. It can live without LOG4J also.
  • If Log4j exists, it chooses it. Otherwise other default available.
  • LOG4J is popular because of it's easy redirecting output to a file, and flexibility to configure log levels.

JavaDoc URL: https://commons.apache.org/proper/commons-logging/javadocs/api-1.1.3/index.html

Upvotes: 1

TMtech
TMtech

Reputation: 1156

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. For example if I write code using commons logging and deploy it on JBoss, the logging is done by log4j, but if I deploy it on WebSphere logging is done by WebSphere's own logging implementation. If I run the same code as a stand alone application it Java's own logging that is used

Upvotes: 8

Ulrich von Poblotzki
Ulrich von Poblotzki

Reputation: 456

Apache Commons Logging is an abstraction for the concrete implementation. It uses log4j, if present and configured. I would use Commons logging in my code and log4j as logging implementation.

Upvotes: 20

Related Questions