Introduction to Java Log components (Common-logging,log4j,slf4j,logback)

Source: Internet
Author: User
Tags log4j

Common-logging

common-logging is a common log interface provided by Apache. The user is free to choose the third party's log component as a concrete implementation, like log4j, or the logging that comes with the JDK, common-logging will automatically find the log library that is actually used when the program runs by dynamic lookup mechanism. Of course, there is a simple logger implementation of common-logging inside, but the function is very weak. So using common-logging is usually used in conjunction with log4j. The advantage of using it is that the code dependency is common-logging rather than log4j, avoids direct coupling to the specific log scheme, and, if necessary, can change the third-party library of the log implementation.

common code for using common-logging:
Import Org.apache.commons.logging.Log; Import org.apache.commons.logging.LogFactory;  Public class A {    privatestatic Log logger = logfactory.getlog (this. GetClass ());} 
Dynamic Lookup 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 logfactory internal loading log system 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 is scanned, and if found, the configuration inside is loaded, using the configuration inside.
    3. Otherwise, look for commons-logging.properties from Classpath and find it to load according to the configuration inside.
    4. Otherwise, use the default configuration: If you can find log4j, the default is to use the log4j implementation, and if not, use the Jdk14logger implementation, and then use the Simplelog implementation provided internally by Commons-logging.
from the above loading process, as long as the introduction of log4j and Classpath configuration log4j.xml, then commons-logging will make log4j use Normal, and the code does not need to rely on any log4j code.

Slf4j

slf4j is all called simple Logging facade for Java,java simply log façade. Similar to Apache Common-logging, is a façade package for different log frameworks that can be accessed with a log implementation without modifying any configuration at deployment time. However, he statically binds the real log library at compile time. When using SLF4J, if you need to use one of the log implementations, then you must select the correct set of SLF4J jar packages (various bridging packages). common code for using slf4j:

Import Org.slf4j.Logger; Import org.slf4j.LoggerFactory;  Public class A {    privatestatic Log logger = logfactory.getlog (this. GetClass ());} 

slf4j Static binding principle : SLF4J will bind the import org.slf4j.impl.StaticLoggerBinder at compile time; This class implements the binding access to the specific log scheme. Any kind of implementation based on SLF4J should have this class. such as: org.slf4j.slf4j-log4j12-1.5.6: Provides an adaptation to the log4j implementation. Note: If there are any two packages that implement SLF4J at the same time, the problem may occur.

Comparison between SLF4J and common-logging

common-logging automatically finds the log library that is actually used when the program runs by using a dynamic lookup mechanism. Because it uses ClassLoader to find and load the underlying log library, a framework like OSGi does not work properly because OSGi's different plugins use their own classloader. This mechanism of OSGI ensures that plug-ins are independent of each other, but it makes Apache common-logging unable to work.

slf4j statically binds the real log library at compile time, so it can be used in OSGi. In addition, SLF4J supports parameterized log strings, avoiding the need to write the IF (logger.isdebugenable ()) that was previously written to reduce the performance loss of string concatenation, and 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.

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.

Logback

Logback is another open source journaling component designed by the founder of Log4j. The 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. Logback-classic is an improved version of 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.

Comparison between log4j and Logback

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.

Introduction to Java Log components (Common-logging,log4j,slf4j,logback)

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.