slf4j usage log

slf4j uses:
http://www.tuicool.com/articles/IfeUfq

Note that the core API provided by the jar version corresponding to

SLF4J is some interfaces and a factory class of LoggerFactory. When using SLF4J, there is no need to specify which specific logging system you intend to use in the code or in the configuration file. You only need to add the specified slf4j-logo.jar package to the project. This is a bit similar to Spring's IOC idea. Use whichever you want, and switch at will.

The characteristics of using slf4j for logging are obvious: 1. If we need to record the underlying class library or component, we can not affect or force the user to choose which logging system. 2. If we use a specific logging system to write code, but one day we need to change to another logging system, if we wrote it with slf4j api before, it is very simple, but if we write it in a specific way later , it is very troublesome to switch.

Example usage in code:

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class Slf4jTest {

// First get the logging object

static private Logger logger = LoggerFactory.getLogger(Slf4jTest.class);

public static void main(String[] args) {

// record error information

logger.error("[info message]");

// record info, and you can also pass in parameters

logger.info("[info message]{},{},{},{}", "abc", false, 123,new Slf4jTest());

// record deubg information

logger.debug("[debug message] ");

// Record the trace information

logger.trace("[trace message]");

System.out.println("hello world");

}

}
Which logging system to use, this can be switched at will, the demonstration is as follows:

1 、Use the simple log system of slf4j:

add: slf4j-simple.jar to the classpath, 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,com.swu.gusi.Slf4jTest@503f0b70

hello world

2. Switch to jdk log system:

replace slf4j-logo.jar with slf4j-jdk.jar in classpath, run The program is as follows:

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

Serious: [info message]

September 20, 2014 12:33:52 PM com.swu.gusi.Slf4jTest main

information: [info message]abc,false,123,com.swu.gusi.Slf4jTest@400da341

hello world

3. Switch to log4j log system :

Replace slf4j-logo.jar with slf4j-log4j.jar in the classpath. Note that when using log4j, you need to configure the log4j.properites file or log4j.xml by yourself. As for the use of log4j, see other documents.

4. Switch to other log system as above.

5. What if there are multiple logging systems in the classpath. A message similar to the following will be displayed (best not to do this):

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,com.swu.gusi.Slf4jTest@10e80317

hello world

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326347296&siteId=291194637