[Java] Exception handling using Commons Logging

Using Commons Logging

Unlike the logging provided by the Java standard library Commons Logging, is a third-party logging library, which is Apachea logging module created by

Commons LoggingThe feature is that it can mount different log systems, and specify the mounted log system through the configuration file. By default, Commons Logginautomatically search for and use Log4j( Log4jis another popular logging system), and Log4juse if not found JDK Logging.

Use Commons Loggingonly needs to deal with two classes, and only two steps:

The first step is to LogFactoryobtain Logthe instance of the class; the second step is to use Logthe method of the instance to log.

The sample code is as follows:

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 error: package org.apache.commons.logging does not exist(cannot find the package org.apache.commons.logging). Because Commons Loggingit is a library provided by a third party, it must be downloaded first. After downloading, unzip, find 到commons-logging-1.2.jarthis file, and put the Java source code Main.javain a directory, such as workthe directory:

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

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

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, use the java command, you must also specify the classpath, the command is as follows:

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

Note that the incoming classpath has two parts: one is ., the other is commons-logging-1.2.jar, separated by ;. .Indicates the current directory. Without this ., the JVM will not search for Main.class in the current directory and will report an error.

If running under Linuxor macOS, note classpaththat the delimiter is not ;, but ::

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

The result of the operation is 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 Logging6 log levels are defined:

FATAL
ERROR
WARNING
INFO
DEBUG
TRACE
默认级别是INFO。

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

// 在静态方法中引用Log:
public class Main {
    
    
    static final Log log = LogFactory.getLog(Main.class);

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

Referenced in an instance method Log, usually define an instance variable:

// 在实例方法中引用Log:
public class Person {
    
    
    protected final Log log = LogFactory.getLog(getClass());

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

Note that the method of obtaining the instance variable log is LogFactory.getLog(getClass())that although it can also be used LogFactory.getLog(Person.class), the former method has a great advantage, that is, subclasses can directly use the log instance. For example:

// 在子类中使用父类实例化的log:
public class Student extends Person {
    
    
    void bar() {
    
    
        log.info("bar");
    }
}

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

In addition, Commons Loggingthe log method, for example , provides a very useful overloaded method info()in addition to the standard one: , which makes logging exceptions even simpler:info(String)info(String, Throwable)

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

Use log.error(String, Throwable)print exception.

summary

Commons Loggingis the most widely used logging module;

Commons LoggingThe API is very simple;

Commons LoggingAdditional logging modules can be automatically detected and used.

If you have gained something after reading, you can also join the discussion in the Knowledge Planet community:

Guess you like

Origin blog.csdn.net/ihero/article/details/132191220