[Z] Java logs, a few things to know (Commons-logging,log4j,slf4j,logback)

Source: Internet
Author: User
Tags log4j

First, the concept

commons-logging: Apache provides the earliest log façade interface. Avoid direct coupling to specific log schemes. Similar to the JDBC API interface, the specific JDBC driver implementation is implemented by the respective database providers. It is decoupled through the unified interface, but it also implements some simple log schemes internally.

log4j: A classic journaling solution. Inside the log system abstraction encapsulated into logger, appender, pattern and other implementations. We can easily implement the management and diversification of the log system through configuration files.

slf4j: All called simple Logging facade for Java:java simply log façade. is a façade package that is provided for different log frameworks. You can access a log implementation by not modifying any configuration at the time of deployment. And commons-loging should have the same intention. Personal feeling is better from the meter, there is no Commons so many unspoken rules. There are also two additional features:

1. Can support multiple parameters, and through {} placeholder to replace, avoid old write logger.isxxxenabled this helpless judgment, bring performance improvement see: Http://www.slf4j.org/faq.html#logging_performance.

2.OSGI mechanism better compatible support

A picture wins thousands of words, a figure on the official website:


From can find, choose still a lot of.

Logback: As a general-purpose, fast and flexible log framework, lack is a complete implementation of the new log system as an alternative to log4j and slf4j. The official network says that it has excellent performance and that execution speed on critical paths is 10 times times log4j and memory consumption is less.

Ii. Some practical application modes of SLF4J--one: slf4j+log4j

SLF4J (Simple Logging facade for Java) is a general-purpose log framework that cannot be called a facade (façade), and the role of the eye is equivalent to Jakarta Commons Logging. Just like the JCL requires the bottom of the log implementation, such as log4j, java.util.logging, simple Logger and so to complete the specific information output, in fact, basically always jcl+log4j a perfect match. SLF4J's original purpose is to support a variety of lower-level log framework implementations, but the best log implementation is still log4j, so this article describes SLF4J's first usage slf4j+log4j.

Required configuration files and component packages, the following three jar files and a properties file are to be placed on the project's ClassPath.

1. Slf4j-api-1.5.11.jar2. Slf4j-log4j12-1.5.11.jar3. Log4j-1.2.15.jar4. Log4j.properties (can also be log4j.xml, in this case with Log4j.propertes)

The first two packages are downloaded at http://www.slf4j.org/download.html, and a package is downloaded in http://logging.apache.org/log4j/1.2/download.html, It doesn't matter if the version number in the package file name is bad.

Log4j.properties before how to write, and now how to write, such as a simple content, only to the console output log information, as follows:

Log4j.rootlogger=debug,console  Log4j.appender.console=org.apache.log4j.consoleappender  Log4j.appender.console.layout=org.apache.log4j.patternlayout  Log4j.appender.console.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss,sss} [%c]-[%p]%m%n  

Third, several practical application modes of SLF4J--one: Slf4j+logback

One of the uses of SLF4J in front is slf4j+log4j, and the combination here is slf4j+logback. No log4j? Is there a better log implementation than log4j? Yes, the answer is Logback. You wouldn't be surprised if you knew that Logback and log4j were the same masters. Logback the log4j and put it into SLF4J's arms, and was identified by the author as a successor to log4j.

Why is called logback, I do not know, anyway not called Brokeback on the line. Here are more than 10 reasons to promote the substitution of log4j with logback: Reasons to prefer logback over log4j, said the big to is faster; good test; close relationship with slf4j, rich documentation, ability to load configuration files automatically, multiple JVMs write a log file, or other I/O errors do not affect program execution, conditional control is added to the configuration file, strong log filtering, stronger log slicing, automatic compression, deletion of log files, and more data information in the exception stack.

Logback is divided into three modules: Logback-core,logback-classic,logback-access. Logback-core is the core; Logback-classic improves the log4j and implements the SLF4J API itself, so using logback you can still use other log implementations, such as the original log4j,java.util.logging, etc. Logback-access makes it easy for you to access log information, such as through HTTP.

It is also important to mention that logback can be better placed in an OSGI environment. OK, simple logback, just to see how to use it.

Required configuration files and component packages, the following three jar files and an XML file are to be placed on the project's ClassPath.

Slf4j-api-1.5.11.jar Logback-core-0.9.20.jar Logback-classic-0.9.20.jar logback.xml or logback-test.xml (similar to Log4j.properties/log4j.xml)

Four, slf4j how to locate the specific use which log

In the application, logger is obtained through the static GetLogger () of the Loggerfactory class. By looking at the code for that class, you can see that the end result is getting loggerfactory through the StaticLoggerBinder.SINGLETON.getLoggerFactory () method, and then, In passing the specific loggerfactory to get logger. Class Org.slf4j.impl.StaticLoggerBinder is not in the Slf4j-api-1.5.2.jar package, looking closely at each jar package corresponding to the specific log system, you will find that the corresponding jar package has a org.slf4j.impl.StaticLogg Erbinder implementation, different implementations return to the log system corresponding to the loggerfactory, so the so-called static binding, so long as the selection of different jar package can be simple and flexible configuration.

[Z] Java logs, a few things to know (Commons-logging,log4j,slf4j,logback)

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.