一、简介
slf4j是一个日志接口,使用了Facade设计模式,通过阅读源码发现其主要用到了类加载机制,把实现类加载到JVM中。
二、测试
2.1 如果只引api接口包,则日志不能正常打印。报错:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2.2 如果引slf4j-api slf4j-simple两个包,则日志可以正常打印。
2.3 如果引slf4j-api、slf4j-simple和slf4j-log4j12三个包,日志可以正常打印,不过会打警告:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/yp-tc-m-2496/.m2/repository/org/slf4j/slf4j-simple/1.7.12/slf4j-simple-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/yp-tc-m-2496/.m2/repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.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]
三、附
3.1 slf4j源码解读见:
http://www.cnblogs.com/xing901022/p/4149524.html
http://www.blogjava.net/DLevin/archive/2012/11/08/390991.html
3.2 类加载机制见:http://www.ibm.com/developerworks/cn/java/j-lo-classloader/
3.3 Facade模式可以百度。
四、如有问题可以留言!