There are many Java logging scenarios, including: Java.util.logging, Apache commons-logging and log4j, slf4j, and Logback. A large project will use a number of third-party jar packages, which may use the various log schemes described above, how to use the combination of slf4j+logback in new projects, so that all other jar package logs are also output to logback, and avoid conflicts and exceptions?
SLF4J is a simple facade for logging systems allowing the end-user-plug-in the desired logging system at deployment Tim E.
Logback is intended as a successor to the popular Log4j project. It was designed by Ceki Gülcü, Log4j ' s founder. It builds upon a decade of experience gained in designing industrial-strength logging systems. The resulting product, i.e logback, is faster and have a smaller footprint than all existing logging systems, sometimes by A wide margin. Just As importantly, Logback offers unique and rather useful features missing in other logging systems
The following diagram is a good illustration of how slf4j and each log framework integrate:
Org.slf4j.LoggerFactory in the Slf4j-api.jar will Org.slf4j.impl.StaticLoggerBinder, this staticloggerbinder in Logback-classic.jar, so Bind SLF4J's log to Logback. At this time, if Classpath also have Slf4j-log4j12.jar then will be reported multiple SLF4J bindings error, because Slf4j-log4j12.jar also have staticloggerbinder implementation.
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/mvn_repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/mvn_repository/org/slf4j/slf4j-log4j12/1.7.6/slf4j-log4j12-1.7.6.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 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Application can only bind slf4j to a final log implementation scheme, can be logback,log4j,comming-logging or java.util.logging, etc., this article mainly introduces SLF4J binding logback, The above diagram is bound to logback.
If the app references other jars that are using log4j, Comming-logging or java.util.logging log, to put these logs are also routed to Logback, the first to route these log calls to Slf4j,slf4j to logback. The implementation is Jcl-over-slf4j.jar replace Comm Ons-logging, replace Log4j.jar with Log4j-over-slf4j.jar, replace java.util.logging with Jul-to-slf4j.jar. The principle is very simple, Log4j-over-slf4j.jar has log4j.jar the same name of the class, so there is Log4j-over-slf4j.jar will log4j.jar deleted. But the principle of jul-to-slf4j.jar is more special, because java.util.logging is the JDK comes with, there is no way to replace, want to know how to do can see the implementation code code.
Maven Pom.xml Configuration:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupid>ch.qos.logback</ groupid> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </depe ndency> <dependency> <groupId>ch.qos.logback</groupId> <artifactid>logback-core</ artifactid> <version>1.1.2</version> </dependency> <dependency> <groupid>org. Slf4j</groupid> <artifactId>log4j-over-slf4j</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <ARTIFACTID>JCL-OVER-SL f4j</artifactid> <version>1.7.6</version> </dependency> <dependency> <groupid >org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>1.7.6</version> </dependency>
Logback Configuration:
<?xml version= "1.0" encoding= "UTF-8"?> <configuration> <!--Send Debug messages to System.out- <appender name= "STDOUT" class= "Ch.qos.logback.core.ConsoleAppender" > <!--by default, Encoders is Assigned the type Ch.qos.logback.classic.encoder.PatternLayoutEncoder-<encoder> <pat Tern>%d{hh:mm:ss. SSS} [%thread]%-5level%logger{5}-%msg%n</pattern> </encoder> </appender> <!-- Send debug messages to a file-to-<appender name= "file" class= "Ch.qos.logback.core.rolling.RollingFile Appender "> <file>D:/logs/java_toolbox_lbk.log</file> <encoder class=" ch.qos.logback.cl Assic.encoder.PatternLayoutEncoder "> <pattern>%d{yyyy-mm-dd_hh:mm:ss. SSS} [%thread]%-5level%logger{36}-%msg%n</pattern> </encoder> <rollingpolicy class= "c H.qos.logback.core.rolling.fixedwinDowrollingpolicy "> <FileNamePattern>D:/logs/java_toolbox_lbk.%i.log.zip</FileNamePattern> <MinIndex>1</MinIndex> <MaxIndex>10</MaxIndex> </ROLLINGPOLICY&G T <triggeringpolicy class= "Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" > <MAXFILESIZE>2MB </MaxFileSize> </triggeringPolicy> </appender> <logger name= "com.weibo.keeplooking . Logging "level=" INFO "additivity=" false "> <appender-ref ref=" STDOUT "/> <appender-ref ref=" FILE "/> </logger> <!--By default, the level of the root level are set to DEBUG-to-<root Level= "DEBUG" > <appender-ref ref= "STDOUT"/> </root> </configuration>
Test code:
Import Org.junit.Test; Import Org.slf4j.bridge.SLF4JBridgeHandler; public class Variouslogging {private static final Java.util.logging.Logger Juclogger = Java.util.logging.Logger.getL Ogger (Variouslogging.class. GetName ()); private static final Org.apache.log4j.Logger Log4jlogger = Org.apache.log4j.Logger.getLogger (Variouslogging.class); private static final Org.apache.commons.logging.Log Commonlogger = org.apache.commons.logging.LogFactory. Get Log (Variouslogging.class); private static final Org.slf4j.Logger Slf4jlogger = Org.slf4j.LoggerFactory.getLogger (Variouslogging.class); static {//Optionally remove existing handlers attached to J.U.L root logger slf4jbridgehandler.removeh Andlersforrootlogger (); (since slf4j 1.6.5)//Add Slf4jbridgehandler to J.U.L ' s root logger, should is done once during//t He initialization phase of your application slf4jbridgehandler.install (); } private void Logwithjuc () {juclogger.info ("Java Util logging ..."); } private void Logwithlog4j () {log4jlogger.info ("log4j ..."); } private void Logwithcommonlogging () {commonlogger.info ("Common logging ..."); } private void Logwithslf4j () {slf4jlogger.info ("slf4j ..."); } @Test public void testlogging () {Logwithjuc (); Logwithlog4j (); Logwithcommonlogging (); LOGWITHSLF4J (); } }
Output Result:
23:16:49.378 [main] INFO C.w.k.l.variouslogging-java util logging ...
23:16:49.382 [main] INFO c.w.k.l.variouslogging-log4j ...
23:16:49.382 [main] INFO C.w.k.l.variouslogging-common logging ...
23:16:49.382 [main] INFO c.w.k.l.variouslogging-slf4j ...
Java logs: integrated slf4j and Logback