springboot工程使用maven-assembly-plugin插件打包程序后,启动报jar包冲突,报错日志如下:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/api_streaming/streaming-api-1.0/lib/logback-classic-1.1.11.jar!/org/slf4j/impl/Static
LoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/api_streaming/streaming-api-1.0/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLo
ggerBinder.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和slf4j-log4j12冲突,不能同时存在logback和slf4j-log4j12,此时程序是可以正常运行,但是日志却无法打印。
解决方法有两种,第一种,排除掉冲突jar包,分两步:
1、mvn -Dverbose dependency:tree 查看jar包的传递依赖,部分显示如下:
[INFO] ± org.springframework.boot:spring-boot-starter-web:jar:1.5.9.RELEASE:compile
[INFO] | ± org.springframework.boot:spring-boot-starter:jar:1.5.9.RELEASE:compile
[INFO] | | ± org.springframework.boot:spring-boot:jar:1.5.9.RELEASE:compile
[INFO] | | | ± (org.springframework:spring-core:jar:4.3.13.RELEASE:compile - omitted for duplicate)
[INFO] | | | - (org.springframework:spring-context:jar:4.3.13.RELEASE:compile - omitted for duplicate)
[INFO] | | ± org.springframework.boot:spring-boot-autoconfigure:jar:1.5.9.RELEASE:compile
[INFO] | | | - (org.springframework.boot:spring-boot:jar:1.5.9.RELEASE:compile - omitted for duplicate)
[INFO] | | ± org.springframework.boot:spring-boot-starter-logging:jar:1.5.9.RELEASE:compile
[INFO] | | | ± ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] | | | | ± ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] | | | | - (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.6.3; omitted for duplicate)
[INFO] | | | ± org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] | | | | - (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.7.22; omitted for duplicate)
[INFO] | | | ± org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | | | - (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.7.22; omitted for duplicate)
[INFO] | | | - org.slf4j:log4j-over-slf4j:jar:1.7.25:compile
[INFO] | | | - (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.7.22; omitted for duplicate)
最后写着compile的就是编译成功的。
最后写着omitted for duplicate的就是有jar包被重复依赖了,但是可以忽略。
在全部输出信息中检索logback和log4j12,将编译成功的logback或者log4j12从dependency中排除掉。
2、exclusion冲突jar包:
logback和log4j12只需要保留其一,如果保留logback则应该将log4j.properties替换成logback.xml。
排除logback:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
排除log4j12:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>${kafka.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
第二种方案:
修改启动脚本,使用nohup命令启动:
nohup "$JAVACMD" $JAVA_OPTS \
-Dbasedir="$BASEDIR" \
-classpath "$CLASSPATH" $CLASS > ${LOG_DIR}/nohup.out 2>&1 < /dev/null &
2>&1 < /dev/null 中2和1表示文件描述符,2表示错误输出,1表示标准输出,整个脚本意思是将错误输出和标准输出均重定向到nohup.out,就是说日志不走log4j.properties的指定,这样就算jar包冲突日志也可以打印出来,只不过所有日志均在nohup.out一个文件中。
如果改成 2>&1 > /dev/null 则表示启动错误日志重定向到nohup.out文件,然后将标准输出重定向到/dev/null (黑洞),这样程序的日志就会输出到log4j.properties指定文件
总结:其实可以看到第二种方案仅限于日志包冲突,所以如果有其他jar包冲突,还是使用第一种方案