The relationship between the Java log commons-logging log4j slf4j

Source: Internet
Author: User

One, before the log operation is generally in a class to add the following code:

Import org.apache.log4j.logger;//introduced the LOG4J package private static final Logger LOG = Logger.getlogger (Test.class);

Second, later saw someone else's code is written like this:

Import Org.apache.commons.logging.log;import org.apache.commons.logging.logfactory;// Introduced is the commons-logging package private static Log logger = Logfactory.getlog (Accountaction.class);

Third, later think of using log4j to put MyBatis SQL statement output also need to add Slf4j-api-x.x.x.jar and Slf4j-log4j12-x.x.x.jar two packets. See so many logs, messy, so on the degree of Niang, a little understanding of the relationship between these logs. Here by the way to give a more superficial summary, crossing have a better understanding can leave a message, we learn progress together.

1) commons-logging and Slf4j-api

Both of these log packages provide a standard interface. Similar to JDBC, give an interface, specifically how to implement your own to play. The difference is that commons-logging itself has a simple implementation. Therefore, in the (b) code, if the introduction of commons-logging does not introduce log4j of the rack package, the log can print normally, the printing effect is as follows:

If you join LOG4J's rack package (you need to configure Log4j.properties or log4j.xml), the log effect is as follows:

In simple terms, Commons-logging will go to find the commons-logging.properties configuration file, look for the specified implementation log, if not, the default to find Log4j (after all, it is home-made things), if not specified, and did not find the log4j rack package implementation, then, Commo Ns-logging on their own, the result is a picture of the effect. There is no default implementation of SLF4J.

2, Slf4j-api and SLF4J-LOG4J12

There must be inconsistencies between the interfaces of Slf4j-api and commons-logging. Some log implementations may not follow the Slf4j-api interface, so the intermediate conversion is one layer. So Slf4j-log4j12 is a taste similar to a converter. Log4j is used after the conversion. Of course there are others, such as Sli4j-slf4j-logback.

Iv. Summary

1) The use of their own projects to introduce Commons-logging and log4j two rack package, and then use (ii) in the Code;

2) Some projects may be used slf4j, such as MyBatis, if you want to continue to use log4j need to introduce slf4j-log4j12;

3) The use of log4j two combinations: "commons-logging+log4j" and "slf4j+slf4j-log4j12+log4j";

This article is for log4j only, and other open source logs are not covered. Rookie's opinion, Daniel do not spray.



The relationship between the Java log commons-logging log4j slf4j

Related Article

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.