Class Path Contains Multiple Slf4j Bindings



Lf4j: class path contains multiple slf4j bindings

Please help to resolve the schematool error $HIVEHOME/bin/schematool -initSchema -dbType mysql SLF4J: Class path contains multiple SLF4J bindings.

  1. I am getting following runtime Exception while running my java code. Could someone please help me resolve the binding conflicts. SLF4J: Class path contains multiple SLF4J bindings.
  2. Springboot项目跑起来后,一直报SLF4J包冲突,看着很烦。Connected to the target VM, address: '127.0.0.1:64448', transport: 'socket'SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in jar:file.

Slf4j: Class Path Contains Multiple Slf4j Bindings

Kung fu tv video. 在Spring Boot集成SLF4j和Log4j2,启动后报错:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/clcaza/jars/majing-admin.jar!/BOOT-INF/lib/log4j-slf4j-impl-2.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/clcaza/jars/majing-admin.jar!/BOOT-INF/lib/logback-classic-1.1.11.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.apache.logging.slf4j.Log4jLoggerFactory]
Exception in thread 'main' java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from jar:file:/home/clcaza/jars/majing-admin.jar!/BOOT-INF/lib/log4j-slf4j-impl-2.7.jar!/). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory

Path

Slf4j Actual Binding Is Of Type

原因及解决

原因是在classpath里有多个SLF4J的绑定,SLF4J最终绑定了org.apache.logging.slf4j.Log4jLoggerFactory。这和Logback冲突导致异常。

集成Log4j2推荐使用starter,它可以很好帮我们处理此冲突,Maven添加以下依赖,并且排除spring-boot-starter-logging