首页  

slf4j集成log4j     所属分类 slf4j 浏览量 1233
slf4j 支持占位符号

添加依赖

<dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.20</version>
</dependency>


log4j 和 slf4j-api 会简介依赖进来

mvn dependency:tree -Dverbose

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ logerror ---
[INFO] dyyx:logerror:jar:1.0
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.20:compile
[INFO]    +- org.slf4j:slf4j-api:jar:1.7.20:compile
[INFO]    \- log4j:log4j:jar:1.2.17:compile

增加log4j配置  log4j.properties
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.properties 不存在则打印warn信息
log4j:WARN No appenders could be found for logger (dyyx.Hello).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


测试代码

import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Hello {
	public static void main(String[] args) throws Exception {
		// System.out.println("hello");
		Logger log = LoggerFactory.getLogger(Hello.class);
		log.error("error,"+new Date());
		log.info("hello,{}","tiger");
	}
}

ERROR [main] (Hello.java:11) - error,Mon Jun 10 12:55:33 CST 2019
 INFO [main] (Hello.java:12) - hello,tiger


日志静态绑定
org/slf4j/impl/StaticLoggerBinder.class

    public static Logger getLogger(String name) {
        ILoggerFactory iLoggerFactory = getILoggerFactory();
        return iLoggerFactory.getLogger(name);
    }

LoggerFactory.bind() 

    private final static void bind() {
        try {
            Set staticLoggerBinderPathSet = null;
            // skip check under android, see also http://jira.qos.ch/browse/SLF4J-328
            if (!isAndroid()) {
                staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
                reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);
            }
            // the next line does the binding
            StaticLoggerBinder.getSingleton();
            INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
            reportActualBinding(staticLoggerBinderPathSet);
            replayEvents();
        } catch (NoClassDefFoundError ncde) {
            String msg = ncde.getMessage();
            if (messageContainsOrgSlf4jImplStaticLoggerBinder(msg)) {
                INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION;
                Util.report("Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".");
                Util.report("Defaulting to no-operation (NOP) logger implementation");
                Util.report("See " + NO_STATICLOGGERBINDER_URL + " for further details.");
            } else {
                failedBinding(ncde);
                throw ncde;
            }
        } catch (java.lang.NoSuchMethodError nsme) {
            String msg = nsme.getMessage();
            if (msg != null && msg.contains("org.slf4j.impl.StaticLoggerBinder.getSingleton()")) {
                INITIALIZATION_STATE = FAILED_INITIALIZATION;
                Util.report("slf4j-api 1.6.x (or later) is incompatible with this binding.");
                Util.report("Your binding is version 1.5.5 or earlier.");
                Util.report("Upgrade your binding to version 1.6.x.");
            }
            throw nsme;
        } catch (Exception e) {
            failedBinding(e);
            throw new IllegalStateException("Unexpected initialization failure", e);
        }
    }
    
 

扫描 org.slf4j.impl.StaticLoggerBinder
有多个话 ,只能绑定一个
该类不存在 
StaticLoggerBinder.getSingleton() 不存在
其他绑定异常


slf4j-log4j12/1.7.20
StaticLoggerBinder

private StaticLoggerBinder() {
        loggerFactory = new Log4jLoggerFactory();
        try {
            @SuppressWarnings("unused")
            Level level = Level.TRACE;
        } catch (NoSuchFieldError nsfe) {
            Util.report("This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version");
        }
    }

上一篇     下一篇
金融圈饭局老司机火眼金睛识人

云原生应用12要素

java日志框架冲突介绍

java日志系统转换

log4j无缝迁移到logback

jdk日志无缝迁移到slf4j