Java log component (relationship and difference between log4j, Logback, slf4j and commons-logging)

Recently, when using the log system to process projects, very strange problems have occurred, so I have a deep understanding of the types and internal connections of the log system, and I will share the point of the total with everyone! I have to admit that when I first learned about it, I would inevitably have a shallow understanding. I hope that the great god can correct me and learn and make progress together

Common log frameworks in development include log4j, slf4j, and commons-logging

1. First introduce the relevant knowledge of these types of logs

commons-logging log component

Common-logging is a common logging interface provided by apache . Users can freely choose third-party log components as specific implementations , such as log4j or jdk's own logging. Common-logging will automatically find out the log library that is actually used when the program is running through a dynamic search mechanism. Of course, there is a simple implementation of Simple logger inside common-logging, but its function is very weak. Therefore, common-logging is usually used in conjunction with log4j. The advantage of using it is that the code depends on common-logging instead of log4j, which avoids direct coupling with specific logging solutions. If necessary, you can change the third-party library implemented by logging.

Common code using common-logging:

package www.supermaster.cn.utils.shiro;

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

/**
 * @ClassName: MyRealm
 * @Description:[Shiro认证模拟]
 * @author: Lance Ting
 * @date: 2018年12月12日 下午9:04:27
 */
public class MyRealm
{
	/**
	 * 日志设置
	 */
	private static Log logger = LogFactory.getLog(MyRealm.class);  
}

Configure in the pom file

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

And log4j.xml is configured on the classpath , commons-logging will make log4j use normally, and the code does not need to rely on any log4j code.

Slf4j log component

The full name of slf4j is Simple Logging Facade for JAVA, java simple log facade. Similar to Apache Common-Logging, it is a facade package (an interface rather than an implementation) provided by different logging frameworks . You can access a log implementation solution without modifying any configuration during deployment. However, the real Log library is statically bound at compile time . When using SLF4J, if you need to use a certain log implementation, then you must choose the correct set of SLF4J jar packages (various bridge packages).

Common code using slf4j:

package www.supermaster.cn.utils.shiro;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: MyRealm
 * @Description:[]
 * @author: Lance Ting
 * @date: 2018年12月12日 下午10:14:21
 */
public class MyRealm extends AuthorizingRealm
{
	/**
	 * 日志设置
	 */
	private static Logger logger = LoggerFactory.getLogger(MyRealm.class);

}

Configure in the pom file

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>

The principle of slf4j static binding: SLF4J will bind org.slf4j.impl.StaticLoggerBinder at compile time; this class implements binding access to specific log schemes. Any implementation based on slf4j must have this class. Such as: org.slf4j.slf4j-log4j12-1.5.6: Provide an adaptation to log4j.

When using slf4j interface involves binder and bridges, logs are bridge slf4j-log4j12-version slf4j bind to log4j, jcl-over-slf4j will use the interface to jcl slf4j interface will then use the binding slf4j The log binding in the device implements to record the log.

The typical collocation of SLF4J is to place the five jars of slf4j-api, JCL bridge, java.util.logging (JUL) bridge, log4j binder, and log4j in the CLASS_PATH. The principle is to use various bridges of slf4j to bridge all log interfaces to the slf4j interface, and then select a specific slf4j binder and log implementer to achieve the purpose of unified output of all logs using one log implementer .

Log4j log component

Log4j is an open source project of Apache. By using Log4j, we can control the destination of log information delivery to consoles, files, GUI components, even socket servers, NT event loggers, UNIX Syslog daemons, etc.; The user can also control the output format of each log; by defining the level of each log information, the user can control the log generation process in more detail. These can be flexibly configured through a configuration file, without the need to modify the program code.

Logback log component

Logback is another open source diary component designed by the founder of log4j.

Logback is currently divided into three modules: logback-core, logback-classic and logback-access.

  • logback-core is the basic module of the other two modules.
  • logback-classic is an improved version of log4j. In addition, logback-classic fully implements the SLF4J API so that you can easily change to other diary systems such as log4j or JDK14 Logging.
  • The logback-access access module integrates with the Servlet container to provide the function of accessing the diary through Http.

 

2. Comparison between slf4j and common-logging

Both slf4j and commons-logging are a log interface of the log component, and the use of logs can be achieved only by borrowing specific implementations.

Common-logging automatically finds out the log library actually used when the program is running through a dynamic search mechanism. Because it uses ClassLoader to find and load the underlying log library, frameworks like OSGI cannot work properly because different plug-ins of OSGI use their own ClassLoader. This mechanism of OSGI ensures that the plug-ins are independent of each other, but it makes Apache Common-Logging unable to work.

slf4j is statically bound to the real Log library at compile time, so it can be used in OSGI. In addition, SLF4J supports parameterized log strings, which avoids the if (logger.isDebugEnable()) that had to be written in order to reduce the performance loss of string splicing. Now you can directly write: logger.debug("current user is : {}”, user). Assembling the message was postponed until it was able to determine whether to display the message, but the cost of obtaining the parameters was not spared.

 

3. Comparison between Log4j and LogBack

Log4j and Logback are both implementations of loggers

LogBack, as a universal, reliable, fast and flexible log framework, will be used as a replacement for Log4j and SLF4J to form a complete implementation of the new log system.

LogBack claims to have excellent performance, "certain key operations, such as the operation to determine whether to record a log statement, its performance has been significantly improved. This operation requires 3 nanoseconds in LogBack, and 30 nanoseconds in Log4J Seconds. LogBack is also faster to create loggers: 13 microseconds, while in Log4J it takes 23 microseconds. More importantly, it only takes 94 nanoseconds to get an existing logger, while Log4J takes 2234 In nanoseconds, the time is reduced to 1/23. The performance improvement compared to JUL is also significant."

In addition, all LOGBack documents are fully provided for free , unlike Log4J, which only provides some free documents and requires users to purchase paid documents.

 

4. The centralized combination form of Slf4j

(1) slf4j + log4j configuration form

<!-- slf4j interface-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
</dependency>

<!-- jcl bridge to slf4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.1</version>
</dependency>

<!-- slf4j binding to log4j  -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.1</version>
</dependency>

<!-- log4j recorder -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

(2) slf4j + logback configuration form

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>  

(3) slf4j + logback + jcl bridge configuration form

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.1</version>
</dependency>

 

Please indicate the source for reprinting https://blog.csdn.net/dgxin_605/article/details/84981033 , thanks for your support!

Guess you like

Origin blog.csdn.net/dgxin_605/article/details/84981033