Introduction to the various components of the java log

For details, please refer to: http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html

 

common-logging

common-logging is a common logging interface provided by apache. Users can freely choose a third-party log component as a specific implementation, such as log4j, or the logging that comes with jdk, and common-logging will automatically find 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 it is very weak. So using 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. When necessary, you can change the third-party library for logging implementation.

 

Common code using common-logging:
[java]  view plain copy
 
  1. import org.apache.commons.logging.Log;  
  2. import org.apache.commons.logging.LogFactory;  
  3.   
  4. public class A {  
  5.     private static Log logger = LogFactory.getLog(this.getClass());  
  6. }  
Dynamic search principle : Log is an interface declaration. The inside of the LogFactory will load the specific log system and obtain the implementation class that implements the Log interface. The process of loading the log system inside the LogFactory is as follows:
  1. First, look for the org.apache.commons.logging.LogFactory property configuration.
  2. Otherwise, using the service discovery mechanism provided by JDK1.3, the META-INF/services/org.apache.commons.logging.LogFactory file under classpah will be scanned, and if found, the configuration inside will be loaded and used.
  3. Otherwise, look for commons-logging.properties from the Classpath, and load it according to the configuration inside.
  4. Otherwise, the default configuration is used: if Log4j can be found, the log4j implementation is used by default, if not, the JDK14Logger implementation is used, and if not, the SimpleLog implementation provided by commons-logging is used.
From the above loading process, as long as log4j is introduced 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

slf4j is called Simple Logging Facade for JAVA, java simple log facade. Similar to Apache Common-Logging, it is a facade encapsulation provided by different logging frameworks. It can access a logging implementation scheme without modifying any configuration during deployment. However, he statically binds the real Log library at compile time. When using SLF4J, if you need to use a certain logging implementation, then you must select the correct set of SLF4J jar packages (various bridge packages).

Common code using slf4j:

[java]  view plain copy
 
  1. import org.slf4j.Logger;  
  2. import org.slf4j.LoggerFactory;  
  3.   
  4. public class A {  
  5.     private static Log logger = LogFactory.getLog(this.getClass());  
  6. }  


slf4j静态绑定原理:SLF4J 会在编译时会绑定import org.slf4j.impl.StaticLoggerBinder; 该类里面实现对具体日志方案的绑定接入。任何一种基于slf4j 的实现都要有一个这个类。如:org.slf4j.slf4j-log4j12-1.5.6: 提供对 log4j 的一种适配实现。注意:如果有任意两个实现slf4j 的包同时出现,那么就可能出现问题。

 

slf4j 与 common-logging 比较

common-logging通过动态查找的机制,在程序运行时自动找出真正使用的日志库。由于它使用了ClassLoader寻找和载入底层的日志库, 导致了象OSGI这样的框架无法正常工作,因为OSGI的不同的插件使用自己的ClassLoader。 OSGI的这种机制保证了插件互相独立,然而却使Apache Common-Logging无法工作。

slf4j编译时静态绑定真正的Log库,因此可以再OSGI中使用。另外,SLF4J 支持参数化的log字符串,避免了之前为了减少字符串拼接的性能损耗而不得不写的if(logger.isDebugEnable()),现在你可以直接写:logger.debug(“current user is: {}”, user)。拼装消息被推迟到了它能够确定是不是要显示这条消息的时候,但是获取参数的代价并没有幸免。

Log4j

An open source project of Apache, by using Log4j, we can control the destination of log information delivery is console, file, GUI component, even socket server, NT event logger, UNIX Syslog daemon, etc.; users can also The output format of each log can be controlled; 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 modifying the program code.

LogBack

Logback is another open source journaling 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 base module for 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 logging systems such as log4j or JDK14 Logging. The logback-access access module integrates with the servlet container to provide access to the log through Http. 

Log4j vs LogBack

As a general, reliable, fast and flexible logging framework, LogBack will be used as a replacement for Log4j and SLF4J to form a complete implementation of the new logging system. LOGBack claims to have excellent performance, "Certain key operations, such as determining whether to log a log statement, have significantly improved performance. This operation takes 3 nanoseconds in LogBack and 30 nanoseconds in Log4J. seconds. LogBack also creates loggers faster: 13 microseconds, compared to 23 microseconds in Log4J. What's more, it takes only 94 nanoseconds to fetch an existing logger, compared to 2234 for Log4J nanoseconds, the time is reduced to 1/23. The performance improvement compared to JUL is also significant.” In addition, all the documents of LOGBack are fully provided for free, unlike Log4J, which only provides some free documents and requires users to purchase paid documents. 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271053&siteId=291194637