The chaotic Java log system

Source: Internet
Author: User
Tags system log log4j

The chaotic Java log system

2016/09/10 | Category: Basic Technologies | 0 Reviews | Tags: LOG

Share to:Original source: Xirong

First, the troubled doubts

The current log framework has JDK logging,log4j1, log4j2, Logback, these frameworks have their own custom log API, and have the corresponding implementation; currently used to implement the unified framework Apache commons-logging, SLF4J, Following the "interface-oriented programming" principle, these two frameworks allow users to select a specific log implementation system (Log4j1\log4j2\logback, etc.) during the program run to record logs, which is a common abstraction of some interfaces.

These log systems involve a variety of miscellaneous integrated jar packages, as follows:

    • log4j, Log4j-api, Log4j-core
    • Log4j-1.2-api, LOG4J-JCL, Log4j-slf4j-impl, Log4j-jul
    • Logback-core, Logback-classic, logback-access
    • Commons-logging
    • Slf4j-api, Slf4j-log4j12, Slf4j-simple, jcl-over-slf4j, Slf4j-jdk14, log4j-over-slf4j, SLF4J-JCL

Log4j
Apache an open source project, through the use of log4j, we can control the destination of the log information delivery is the console, files, GUI components, even the socket server, NT Event recorder, UNIX syslog daemon, etc., the user can also control the output format of each log By defining the level of each log information, the user is able to control the log generation process more carefully. These can be configured flexibly with a single configuration file without the need to modify the program code. The latest current version for log4j 1.2 MAVEN relies on the following:

12345 <DEPENDENCY> &NBSP;&NBSP;&NBSP;&NBSP; <GROUPID>LOG4J</GROUPID>       <ARTIFACTID>LOG4J</ARTIFACTID> &NBSP;&NBSP;&NBSP;&NBSP; <VERSION> 1.2 17 </VERSION> </DEPENDENCY>

Logback
Logback is another open source journaling component designed by LOG4J founder, Logback is currently divided into three modules: Logback-core,logback-classic and logback-access. The Logback-core is the base module for the other two modules, and the Logback-classic is an improved version of the log4j. In addition logback-classic full implementation of the SLF4J API allows you to easily change to other journaling systems such as log4j or JDK14 Logging. The Logback-access Access module integrates with the servlet container to provide the ability to access journaling over HTTP. The latest MAVEN dependencies are as follows:

123456789101112131415 <dependency>    <groupId>ch.qos.logback</groupId>    <artifactId>logback-core</artifactId>    <version>1.1.6</version></dependency><dependency>    <groupId>ch.qos.logback</groupId>    <artifactId>logback-classic</artifactId>    <version>1.1.6</version></dependency><dependency>    <groupId>org.slf4j</groupId>    <artifactId>slf4j-api</artifactId>    <version>1.7.18</version></dependency>

As a general-purpose, fast and flexible log framework, Logback is a complete implementation of the new log system as an alternative to log4j and slf4j. Logback claims to have excellent performance, "some key operations, such as determining whether to record a log statement operation, its performance has been significantly improved." This operation requires 3 nanoseconds in Logback and 30 nanoseconds in log4j. The logback creates a logger (logger) faster: 13 microseconds, while in log4j it takes 23 microseconds. What's more, it takes only 94 nanoseconds to get the existing logger, and the log4j takes 2234 nanoseconds and the time is reduced to 1/23. Performance improvements compared to Jul are also significant. " In addition, all logback documents are provided free of charge, unlike log4j, which only provides some free documentation and requires users to purchase paid documents. Details include:

    • Faster execution speed
    • More fully tested
    • Logback-classic a very natural realization of the SLF4J
    • Using an XML configuration file or groovy
    • Automatically reload configuration files
    • Gracefully recover from I/O errors
    • Automatic purging of old log archive files
    • Automatically compress archived log files
    • Caution Mode
    • Lilith
    • Conditional processing in a configuration file
    • Richer filtration
    • The Logback-access module, which provides the ability to access logs via HTTP, is an integral part of Logback

The detailed introduction can refer to: http://www.oschina.net/translate/reasons-to-prefer-logbak-over-log4j

More and more open source projects rely on Logback:

Slf4j
The simple journal façade (facade) SLF4J provides a simple, unified interface for various loging APIs, enabling end users to configure their desired loging APIs implementation at deployment time. Logging API implementation can choose to directly implement SLF4J loging APIs such as: Nlog4j, Simplelogger. It is also possible to develop appropriate adapters such as Log4jloggeradapter, jdk14loggeradapter through the API implementations provided by SLF4J.

Apache common-logging
The Java Log façade library is now widely used. The dynamic lookup mechanism automatically finds the log library that is actually used when the program runs. But because it uses ClassLoader to find and load the underlying log library, the framework like OSGi does not work properly due to its different plugins using its own classloader. This mechanism of OSGI ensures that plug-ins are independent of each other, but the Apache common-logging does not work.

The SLF4J library resembles Apache common-logging. However, he statically binds the real log library at compile time. When using SLF4J, if you need to use one of the log implementations, you must select the correct set of SLF4J jar packages so that you can use them in OSGi. In addition, SLF4J supports parameterized log strings, avoids having to write before to reduce the performance loss of string concatenation, and if(logger.isDebugEnable()) now you can write directly: logger.debug(“current user is: {}”, user) . The assembly message was postponed until it was able to determine whether to display the message, but the cost of acquiring the parameter was not spared. At the same time, if there are more than three parameters in the log, the parameters need to be passed in as an array, such as:

12 Object[] params = {value1, value2, value3};logger.debug(“first value: {}, second value: {} and third value: {}.”, params);

Now, more and more open source projects such as Hibernate, Jetty, Spring-osgi, wicket and Mina have migrated to slf4j, so the influence of slf4j cannot be ignored.

Second, how to use

After the introduction of these log framework, then how to use these things, what is the dependency between, and the start of the initialization process exactly what to do, how to find the corresponding implementation class and configuration?

These four articles are the most detailed introduction I have found on the Internet, please refer to:

    1. Jdk-logging, log4j, Logback Log Introduction and principle introduction of the implementation mechanism of three log system
    2. Commons-logging and Jdk-logging, Log4j1, log4j2, Logback Principles of Apache commons-logging General log framework and Logging system introduction
    3. SLF4J and Jdk-logging, Log4j1, log4j2, Logback Integration Principle SLF4J General log framework and specific log implementation mechanism of the system, including the dependent jar package, jar conflict processing;
    4. SLF4J, JCL, Jul, log4j1, log4j2, logback Large summary of the various components of the jar package and the current system log needs to switch the way the implementation method, recommended reading.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 <!--  =================================================  --><!--  日志及相关依赖(用slf4j+logback代替jcl+log4j)  --><!--  =================================================  --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.7</version></dependency><!-- 强制使用 logback的绑定 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.1.3</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.1.3</version></dependency><!-- 强制使用 logback的绑定,这里去除对log4j 的绑定 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>99.0-does-not-exist</version></dependency><!-- slf4j 的桥接器,将第三方类库对 log4j 的调用 delegate 到 slf api 上 --><!-- 这个桥接器是自己做的,主要是我们依赖的类库存在很多硬编码的引用 --><dependency><groupId>org.slf4j</groupId><artifactId>log4j-over-slf4j</artifactId><version>1.7.14-SNAPSHOT</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>1.7.7</version></dependency><!-- 强制排除 log4j 的依赖,全部 delegate 到 log4j-over-slf4j 上 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>99.0-does-not-exist</version></dependency><dependency><groupId>apache-log4j</groupId><artifactId>log4j</artifactId><version>999-not-exist</version></dependency><!-- slf logback 配置结束 -->
Third, the end

Through reading above, the "chaotic Java log System" should have a clearer understanding, but there is really a project need to switch the log system situation? As I understand it, a lot of times when your systems are getting bigger and more complex and need to be refactored to solve performance, consider upgrading your current log configuration. If you are a newly-enabled project, consider using it directly Logback + SLF4j .

Resources

    • Apache Logging Services
    • Logback Home Page
    • Java Log framework: SLF4J, Apache common-logging, log4j and Logback
    • Jdk-logging, LOG4J, Logback Journal Introduction and Principle

The chaotic Java log system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.