Use slf4j instead of Apache common-logging to write logs

Source: Internet
Author: User

Use slf4j instead of Apache common-logging to write logsApache common-logging is a widely used Java log facade library. I have always used it to write logs with log4j. Apache common-logging uses the dynamic search mechanism to automatically find the logstore in use when the program is running. Apache common-logging always works well. It wasn't working until recently when I wrote the osgi plug-in. The reason is that Apache common-logging uses classloader to search for and load the underlying logstore. In osgi, different plug-ins use their own classloader. Classloader of a thread has different execution capabilities when executing different plug-ins. This mechanism of osgi ensures that the plug-ins are independent from each other, but does make Apache common-logging unable to work! The solution is to use the new logstore slf4j. The slf4j library is similar to Apache common-logging. However, he binds the real log library statically during compilation. When using slf4j, if you need to use a log implementation, You must select the correct set of slf4j jar packages. This is indeed a little troublesome, but it is finally possible to develop logs in osgi. In fact, this is not a hassle. The process of using the commonlog interface and actually implemented by slf4j and log4j1. Use it as usual in the project ImportOrg. Apache. commons. Logging. log; ImportOrg. Apache. commons. Logging. logfactory; write logs. 2. Use the log4j. properties file under SRC for configuration. 3, use all jar files: 1) log4j-1.2.15.jar this is the log4j library. Slf4j does not change the underlying implementation library. 2) slf4j-api-1.5.2.jar this is the slf4j library. 3) slf4j-log4j12-1.5.2.jar this includes the log4j adapter and the underlying implementation of static binding log4j. 4) jcl-over-slf4j-1.5.2.jar this provides the commons-logging interface, and the use of common-Loggin interfaces, the underlying is determined by slf4j which implementation mechanism. Here, we need to use the log4j native library, but do not need the commons-logging native library. OK. Copy the preceding four jar packages to lib and import them to the project. As before, you can continue to use Apache common-logging to write logs. If you prefer to use the slf4j portal class or non-log4j log implementation library. You just need to throw the required jar package into your project like above!

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.