Java Log framework and log system

Source: Internet
Author: User

As projects become more complex, no one can guarantee that their projects will run without errors, and errors are not scary, and the problem is to eliminate them in a timely manner, making the project more robust and continuing to run. Troubleshooting These errors requires getting an error message, where the information comes from, a well-designed project, and certainly a log of the project's running. More importantly, it can also complete tracking debugging, program status record, crash data recovery and so on. So how do we design this log class?

In the Java World, log classes are generally not to be considered and designed, as there are many excellent log systems, such as common log4j and Logback, and sun-provided java.util.Logging.

But the log system is more, it is not a good thing! Because in the Java EE Project, we will introduce a lot of third-party packages, such as spring, Mybatis, httpclient and so on ... Each third party's package will have its own log system, the problem comes, if the log system is incompatible or even conflict, disaster arises? or a different log system print the rules of the log, the interface is different, the user will do a variety of adaptation ...

But.... This is impossible!!!!

The log framework solves these problems!!!

What is a log framework?

First look at the different definitions of the two!!!

Log System: the specific implementation of the log. The classics have the Log4j;jdk own java.util.Logging; There are highly rated logback and so on log4j author's introduction;

Log Frame: If there is only one log system, the log frame is completely unnecessary (Logback cannot be used independently), but it backfired. In order to solve the compatibility problem of multiple log systems, the log framework emerges. The mainstream log framework has commons-logging and sl4j;

Commons-logging is an Apache-introduced log framework that commons-logging only specifies the interface specification for the log, which is designed to resemble the design of the servlet and JDBC in the JDK.

The mainstream log system implements the interface defined by the commons-logging, so it looks at the log that is used by the commons-logging, rather than the type of log system that the relationship uses.

Log frame

1, commons-logging:common-logging is a common log interface provided by Apache. Users are free to choose third-party log components as a concrete implementation, such as log4j, or the JDK's own logging,common-logging through the mechanism of dynamic lookups, when the program runs automatically find the real use of the log library.

COMMONS-LOGGING+LOG4J is a classic log implementation scenario. appear in various frameworks. such as spring, WEBX, Ibatis and so on. Generally in order to avoid direct reliance on specific log implementations, is generally combined with commons-logging to achieve. The common code is as follows:

Import Org.apache.commons.logging.Log; Import org.apache.commons.logging.LogFactory; Private Static Log logger = Logfactory.getlog (XXX.  Class);

2. SL4J: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.

LOGBACK+SL4J is also a classic log implementation scenario. 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 must be used in conjunction with SL4J. Since Logback and sl4j are the same authors, their compatibility is self-evident. However, sl4j faces compatibility issues with other log frameworks and log systems. The common code is as follows:

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

Reference: http://blog.csdn.net/yycdaizi/article/details/8276265

Java Log framework and 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.