slf4j+logback

SLF4J is a simple facade for logging systems allowing the end-user to plug-in the desired logging system at deployment time.

详见:http://www.slf4j.org

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 has 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.

详见:http://logback.qos.ch

用slf4j可以把原来用其他日志框架写的日志redirect到logback,具体原理这张图可以很好的说明:

slf4j+logback_第1张图片

slf4j-api.jar里的org.slf4j.LoggerFactory会调org.slf4j.impl.StaticLoggerBinder,这个StaticLoggerBinder在logback-classic.jar里实现,这样就把slf4j的日志绑定到logback了。这时,如果classpath里同时还有slf4j-log4j12.jar那么会报multiple SLF4j bindings错误,因为slf4j-log4j12.jar里也有StaticLoggerBinder实现

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]

注:应用里只能把slf4j绑定到一个最终日志实现方案,可以是logback,log4j,comming-logging或java.util.logging等,本篇主要介绍slf4j绑定logback, 上面这个图就是绑定到logback.

如果应用引用其他的jar是使用log4j,comming-logging或java.util.logging记日志的,要把这些日志也路由到logback上来,首先把这些日志调用路由到slf4j,slf4j再到logback.具体实现是用jcl-over-slf4j.jar替换log4j.jar, 用log4j-over-slf4j.jar替换commons-logging.jar, 用jul-to-slf4j.jar替换java.util.logging. 原理也很简单,jcl-over-slf4j.jar里有log4j.jar同名的类,所以有jcl-over-slf4j.jar了就要把log4j.jar删了。其中jul-to-slf4j.jar比较特殊一些,因为java.util.logging是jdk自带的,没有办法替换,jul-to-slf4j.jar做法比较特殊,详见代码。

maven pom.xml配置:


	org.slf4j
	slf4j-api
	1.7.6


	ch.qos.logback
	logback-classic
	1.1.2


	ch.qos.logback
	logback-core
	1.1.2


	org.slf4j
	log4j-over-slf4j
	1.7.6


	org.slf4j
	jcl-over-slf4j
	1.7.6


	org.slf4j
	jul-to-slf4j
	1.7.6

测试代码:

import org.junit.Test;
import org.slf4j.bridge.SLF4JBridgeHandler;

public class VariousLogging {

    private static final java.util.logging.Logger jucLogger = java.util.logging.Logger.getLogger(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
            .getLog(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.removeHandlersForRootLogger(); // (since SLF4J 1.6.5)

        // add SLF4JBridgeHandler to j.u.l's root logger, should be done once during
        // the 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();
    }

}


logback.xml配置:




	
	
		
		
			%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
		
	

	
	
		D:/logs/java_toolbox_lbk.log
		
			%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
		
		
			D:/logs/java_toolbox_lbk.%i.log.zip
			1
			10
		
		
			2MB
		
	

	
		
		
	

	
	
		
	


输出:

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...

<完>

你可能感兴趣的:(工作)