解决工程中多种日志框架冲突

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/totally123/article/details/81563447

日常开发中,在代码中加日志是必不可少的。然而,由于引用很多第三方的包,而这些第三方包所用的日志框架和版本又不尽相同,比如有log4j、log4j2、logback和Java自带的Logging。

为了不强制依赖某个日志框架,我们会使用slf4j(The Simple Logging Facade for Java),其实slf4j只是一个日志标准,并不是日志系统的具体实现。slf4j只做两件事情:

  • 提供日志接口

  • 提供获取具体日志对象的方法

而slf4j-simple、logback都是slf4j的具体实现,log4j并不直接实现slf4j,但是有专门的一层桥接slf4j-log4j12来实现slf4j。

工程中有多个日志框架存在,在启动时就会有警告信息,如:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/OperationSVN/项目工程/bi-gate/target/bi-gate-1.0.0/WEB-INF/lib/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/OperationSVN/项目工程/bi-gate/target/bi-gate-1.0.0/WEB-INF/lib/slf4j-log4j12-1.7.5.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]

此警告信息表示工程中有logback和log4j两种日志框架,最后slf4j选择了logback。那么该问题如何解决呢,可以看出日志中也打印出来了问题说明的网址,见http://www.slf4j.org/codes.html#multiple_bindings

slf4j在类路径发现多个绑定

总结一下,若不想使用某个日志框架,就在依赖中排除该框架的依赖。比如示例中,工程依赖了cassandra-all,而cassandra-all内部使用了log4j作为日志框架,若我们工程不想使用log4j,就需要排除log4j和slf4j-log4j12两个依赖。

猜你喜欢

转载自blog.csdn.net/totally123/article/details/81563447