Skip to content
On this page

Commons Logging

Different from the logs provided by the Java standard library, Commons Logging is a third-party logging library, which is a logging module created by Apache.

The characteristic of Commons Logging is that it can hook up different logging systems and specify the hooked logging system through the configuration file. By default, Commons Loggin automatically searches for and uses Log4j (Log4j is another popular logging system). If Log4j is not found, JDK Logging is used.

Using Commons Logging only requires dealing with two classes and only two steps:

The first step is to obtain an instance of the Log class through LogFactory ; the second step is to log using the Log instance method.

The sample code is as follows:

java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Main {
  public static void main(String[] args) {
    Log log = LogFactory.getLog(Main.class);
    log.info("start...");
    log.warn("end.");
  }
}

Running the above code, you will definitely get a compilation error, similar to error: package org.apache.commons.logging does not exist (The org.apache.commons.logging package cannot be found). Because Commons Logging is a library provided by a third party, it must be downloaded first. After downloading, unzip it, find the file commons-logging-1.2.jar , and then put the Java source code Main.java in a directory, such as the work directory:

work
├─ commons-logging-1.2.jar
└─ Main.java

Then use javac to compile Main.java . You must specify classpath when compiling, otherwise the compiler cannot find org.apache.commons.logging package we reference. The compilation command is as follows:

sh
javac -cp commons-logging-1.2.jar Main.java

If the compilation is successful, there will be an additional Main.class file in the current directory:

work
├─ commons-logging-1.2.jar
├─ Main.java
└─ Main.class

Now you can execute this Main.class and use the java command. classpath must also be specified. The command is as follows:

sh
java -cp .;commons-logging-1.2.jar Main

Notice that the incoming classpath has two parts: one is . and the other is commons-logging-1.2.jar , separated by ; . . indicates the current directory. If there is no . , the JVM will not search for Main.class in the current directory and will report an error.

If running under Linux or macOS, note that classpath delimiter is not ; but :

sh
java -cp .:commons-logging-1.2.jar Main

The running results are as follows:

Mar 02, 2019 7:15:31 PM Main main
INFO: start...
Mar 02, 2019 7:15:31 PM Main main
WARNING: end.

Commons Logging defines 6 log levels:

  • FATAL
  • ERROR
  • WARNING
  • INFO
  • DEBUG
  • TRACE

The default level is INFO .

When using Commons Logging, if you reference Log in a static method, you usually define a static type variable directly:

java
public class Main {
    static final Log log = LogFactory.getLog(Main.class);

    static void foo() {
        log.info("foo");
    }
}

To reference Log in an instance method, usually define an instance variable:

java
public class Person {
    protected final Log log = LogFactory.getLog(getClass());

    void foo() {
        log.info("foo");
    }
}

Note that the way to obtain the instance variable log is LogFactory.getLog(getClass()) , although it can also be used LogFactory.getLog(Person.class) , but the former method has a very big advantage, that is, subclasses can directly use the log instance. For example:

java
public class Student extends Person {
    void bar() {
        log.info("bar");
    }
}

Due to the dynamic nature of Java classes, the log field obtained by the subclass is actually equivalent to LogFactory.getLog(Student.class) , but it is inherited from the parent class and no code changes are required.

In addition, Commons Logging's logging methods, such as info() , in addition to the standard info(String) , also provide a very useful overloaded method: info(String, Throwable) , which makes logging exceptions simpler:

java
try {
    ...
} catch (Exception e) {
    log.error("got exception!", e);
}

Practise

Use log.error(String, Throwable) to print exceptions.

Summary

Commons Logging is the most widely used logging module;

The API of Commons Logging is very simple;

Commons Logging can automatically detect and use other logging modules.

Commons Logging has loaded