SLF4J (Simple logging facade for Java)

Source: Internet
Author: User

Http://www.tuicool.com/articles/IfeUfq

SLF4J (Simple logging facade for Java) is Java's easy log façade, which is not a specific log solution, it only serves a variety of Slf4j-logo

Log system. Here the Slf4j-logo can be log4j, can be the JDK log, can be logback, it can be slf4j-simple and so on. Officially, SLF4J is a simple facade for the log system, allowing end users to use the log system they want when they deploy their apps. In fact, there is a design pattern (facade design mode, façade design mode). Such as:

In fact, the core API provided by SLF4J is some interface and a loggerfactory factory class. When using SLF4J, you do not need to specify in the code or in the configuration file that you intend to use the specific log system. Just add the Slf4j-logo.jar package to the project. This and spring IOC thought a bit like, want to use which to use which, arbitrarily switches.

The characteristics of using slf4j as a way to log logs are obvious:

1, if we need to record the underlying class library or component of such things, it can not affect or force the user to choose which kind of log system.

2, if we write the code with a particular log system, but one day asked for another log system, if the previous use of SLF4J API written, it is very simple, if written in a specific way, then the switch is very troublesome.





What kind of log system to use, this can switch freely, the demo is as follows:

1. Simple log system using SLF4J:

In Classpath, add: Slf4j-simple.jar, run the program input as follows:

[Main] ERROR com.swu.gusi.Slf4jTest-[Info message]

[Main] Info com.swu.gusi.Slf4jTest-[info message]abc,false,123,[email protected]

Hello World

2. Switch to the JDK log system:

Change Slf4j-logo.jar to Slf4j-jdk.jar in Classpath and run the following program:

September 20, 2014 12:33:52 pm com.swu.gusi.Slf4jTest Main

Severity: [Info message]

September 20, 2014 12:33:52 pm com.swu.gusi.Slf4jTest Main

Info: [Info message]abc,false,123,[email protected]

Hello World

3, switch to log4j log system:

Replace Slf4j-logo.jar with Slf4j-log4j.jar in classpath, note that you need to configure Log4j.properites files or Log4j.xml when using log4j. For log4j use consult other documents.

4, switch to the other log system and the same above.

5, if there are a variety of classpath in the log system, what will happen. You will be prompted with information similar to the following (preferably not):

Slf4j:class path contains multiple slf4j bindings.

Slf4j:found Binding in [jar:file:/e:/repository/org/slf4j/slf4j-simple/1.7.7/slf4j-simple-1.7.7.jar!/org/slf4j/ Impl/staticloggerbinder.class]

Slf4j:found Binding in [Jar:file:/e:/repository/org/slf4j/slf4j-jdk14/1.7.7/slf4j-jdk14-1.7.7.jar!/org/slf4j/impl /staticloggerbinder.class]

Slf4j:see http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

Slf4j:actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]

[Main] ERROR com.swu.gusi.Slf4jTest-[Info message]

[Main] Info com.swu.gusi.Slf4jTest-[info message]abc,false,123,[email protected]

Hello World

Company specifications

http://wiki.corp.xxxxxx.com/pages/viewpage.action?pageId=63243043

SLF4J (Simple logging facade for Java)

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.