ActiveMQ与Logback日志组件SLF4J冲突导致日志不输出

ActiveMQ与Logback中的SLF4J日志组件冲突导致日志不输出,控制台提示 Class path contains multiple SLF4J bindings 的解决方案。

近期码的时候发现logback的组件日志都没输出了,没有报错也就没去管它,直到排错越来越蛋疼……

查看控制台输出,当添加 ActiveMQ maven 依赖的时候日志就不输出了,并且提示

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/asus/.m2/repository/org/apache/activemq/activemq-all/5.9.0/activemq-all-5.9.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/asus/.m2/repository/org/slf4j/slf4j-log4j12/1.7.5/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 [org.slf4j.impl.Log4jLoggerFactory]

很明显,组件冲突了,顺着控制台提示的链接去,有这么一大段说明:

Multiple bindings were found on the class path

SLF4J API is designed to bind with one and only one underlying logging framework at a time. If more than one binding is present on the class path, SLF4J will emit a warning, listing the location of those bindings.
When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. For example, if you have both slf4j-simple-1.7.13.jar and slf4j-nop-1.7.13.jar on the class path and you wish to use the nop (no-operation) binding, then remove slf4j-simple-1.7.13.jar from the class path.
The list of locations that SLF4J provides in this warning usually provides sufficient information to identify the dependency transitively pulling in an unwanted SLF4J binding into your project. In your project’s pom.xml file, exclude this SLF4J binding when declaring the unscrupulous dependency. For example, cassandra-all version 0.8.1 declares both log4j and slf4j-log4j12 as compile-time dependencies. Thus, when you include cassandra-all as a dependency in your project, the cassandra-all declaration will cause both slf4j-log4j12.jar and log4j.jar to be pulled in as dependencies. In case you do not wish to use log4j as the the SLF4J backend, you can instruct Maven to exclude these two artifacts as shown next:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< dependencies >
   < dependency >
     < groupId > org.apache.cassandra</ groupId >
     < artifactId >cassandra-all</ artifactId >
     < version >0.8.1</ version >
 
     < exclusions >
       < exclusion >
         < groupId >org.slf4j</ groupId >
         < artifactId >slf4j-log4j12</ artifactId >
       </ exclusion >
       < exclusion >
         < groupId >log4j</ groupId >
         < artifactId >log4j</ artifactId >
       </ exclusion >
     </ exclusions >
 
   </ dependency >
</ dependencies >

NOTE The warning emitted by SLF4J is just that, a warning. Even when multiple bindings are present, SLF4J will pick one logging framework/implementation and bind with it. The way SLF4J picks a binding is determined by the JVM and for all practical purposes should be considered random. As of version 1.6.6, SLF4J will name the framework/implementation class it is actually bound to.
Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J’s purpose. When you come across an embedded component declaring a compile-time dependency on any SLF4J binding, please take the time to contact the authors of said component/library and kindly ask them to mend their ways.

可以看到,SLF4J 只能绑定到一个日志框架,如果在类路径发现多个绑定器,SLF4J 将警告并且列出这些绑定器。当有多个绑定器的时候SLF4J 将随机选择一个来使用,一般情况下可以在pom文件使用exclusion标签来排除冲突的日志组件,如上方的XML代码,不过在我这边就用不了这个方法了,先看看相关依赖代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!-- Log Support -->
< dependency >
     < groupId >org.slf4j</ groupId >
     < artifactId >slf4j-api</ artifactId >
     < version >1.7.5</ version >
</ dependency >
 
< dependency >
     < groupId >org.slf4j</ groupId >
     < artifactId >jcl-over-slf4j</ artifactId >
     < version >1.7.5</ version >
</ dependency >
 
 
< dependency >
     < groupId >ch.qos.logback</ groupId >
     < artifactId >logback-core</ artifactId >
     < version >1.0.13</ version >
</ dependency >
 
< dependency >
     < groupId >ch.qos.logback</ groupId >
     < artifactId >logback-classic</ artifactId >
     < version >1.0.13</ version >
</ dependency >
 
<!-- jms -->
< dependency >
     < groupId >org.apache.activemq</ groupId >
     < artifactId >activemq-all</ artifactId >
     < version >5.11.0</ version >
</ dependency >
< dependency >
     < groupId >org.springframework</ groupId >
     < artifactId >spring-jms</ artifactId >
     < version >4.1.4.RELEASE</ version >
</ dependency >

查看了eclipse的maven依赖目录才发现,activemq-all这个依赖直接把maven的全部组件给打成一个包了,其中就包括了导致冲突的日志组件,没办法了,只能一个个添加activemq的组件依赖了,修改后的activemq依赖配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- jms -->
< dependency >
     < groupId >org.apache.activemq</ groupId >
     < artifactId >activemq-core</ artifactId >
     < version >5.7.0</ version >
</ dependency >
< dependency >
     < groupId >org.apache.activemq</ groupId >
     < artifactId >activemq-pool</ artifactId >
     < version >5.13.0</ version >
</ dependency >
< dependency >
     < groupId >org.springframework</ groupId >
     < artifactId >spring-jms</ artifactId >
     < version >4.1.4.RELEASE</ version >
</ dependency >

ActiveMQ必备Jars说明:http://activemq.apache.org/initial-configuration.html#InitialConfiguration-RequiredJARs

重启tomcat,久违的日志出现…

来源:https://www.xssfox.com/2016-01-11/activemq%E4%B8%8Elogback%E6%97%A5%E5%BF%97%E7%BB%84%E4%BB%B6slf4j%E5%86%B2%E7%AA%81%E5%AF%BC%E8%87%B4%E6%97%A5%E5%BF%97%E4%B8%8D%E8%BE%93%E5%87%BA/

猜你喜欢

转载自blog.csdn.net/carychengzubin/article/details/53326158
今日推荐