Brief Introduction to commons-logging

Posted by Jackanape on Sat, 18 May 2019 22:09:13 +0200

Not original, turn from https://blog.csdn.net/backbug/article/details/78655664

Brief Introduction to commons-logging

Introduction:

Jakarta Commons-logging (JCL) is the earliest log facade interface provided by apache. Provides simple log implementation and log decoupling.

JCL can choose whether to use Log4j (or other such as slf4j) or JDK Logging, but it does not rely on the API of Log4j, JDK Logging. If the classpath of the project contains a class library of log4j, log4j will be used, otherwise JDK Logging will be used. Using commons-logging allows you to choose which log formats to use flexibly, without modifying the source code. (API interface similar to JDBC)

Principle:

JCL has two basic Abstract classes: Log (basic logger) and LogFactory (responsible for creating Log instances). When commons-logging.jar is added to CLASSPATH, it will reasonably guess the log tool you want to use, and then set itself up. Users do not need to make any settings at all. The default LogFactory is to discover and determine which logging tool will be used (in order, the search process will stop when the first tool is found):

Firstly, the commons-logging.properties file is searched under the classpath. If found, the Log implementation class defined therein is used; if not found, the Log implementation class defined by the system environment variable org.apache.commons.logging.Log is used to find if the system environment variable org.apache.commons.logging.Log has been defined.

 

(2) Check if there is a Log4j package in the classpath, and if it is found, log 4J is automatically used as the log implementation class;

 

(3) Otherwise, use JDK's own log implementation class (after JDK 1.4 only log implementation class);

 

(4) Otherwise, SimpleLog is implemented using a simple log provided by commons-logging itself.

The specific implementation of org.apache.commons.logging.Log is as follows:

- org.apache.commons.logging.impl.Jdk14Logger uses JDK1.4.

Log4J Logger is used by org. apache. commons. logging. impl. Log4J Logger.

org.apache.commons.logging.impl.LogKitLogger uses avalon-Logkit.

- - org.apache.commons.logging.impl.SimpleLog. common-logging comes with its own log implementation class.

- - org.apache.commons.logging.impl.NoOpLog. common-logging comes with its own log implementation class. It implements the Log interface. Its method of exporting logs does not perform any operations.

————————————————————————————————————————

Maven Dependence:

<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.2</version>
</dependency>

 

1. Commons-logging simple log implementation:

(1) Create a new commons-logging.properties file and place it in the root path of the classpath:

 

org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog

 

(2) Use in code

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public class CommonsTest {
	private final static Log logger = LogFactory.getLog(CommonsTest.class);
 
	public static void main(String[] args) {
		logger.debug("DEBUG ...");
		logger.info("INFO ...");
		logger.error("ERROR ...");
	}
}


Output:

 

2. Decoupling function of Commons-logging:

The most important and useful function of commons-logging is decoupling. Its SimpleLog implementation performance is not as good as other implementations, such as log4j.  

(1) Additional dependency

<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.2</version>
</dependency>
<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.17</version>
</dependency>

 

(2) Modify the configuration file:

Modify the commons-logging.properties file: Specify log4j visually

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

And add log4j.properties configuration file:

log4j.rootLogger=DEBUG,console
 
# Output to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
# Setting Output Style    
log4j.appender.console.layout=org.apache.log4j.PatternLayout
# The format of log output information is   
log4j.appender.console.layout.ConversionPattern=[%-d{yyyy-MM-dd HH:mm:ss}]-[%t-%5p]-[%C-%M(%L)]: %m%n

 

(3) Use in code

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public class CommonsTest {
	private final static Log logger = LogFactory.getLog(CommonsTest.class);
 
	public static void main(String[] args) {
		logger.debug("DEBUG ...");
		logger.info("INFO ...");
		logger.error("ERROR ...");
	}
}


Output:

Topics: Apache log4j JDK JDBC