[已解决] SLF4J: Class path contains multiple SLF4J bindings

前言

如题,这属于包含多个日志依赖的问题,话不多说,迅速展示解决方案。

1 问题场景

我的Java项目依赖一个别人写的项目A(不在maven仓库里面),所以我得先在项目A目录下运行mvn install,如下:

mvn clean package -DskipTests

mvn install:install-file -Dfile=/home/apr/xxx-0.0.1-SNAPSHOT-jar-with-dependencies.jar -DgroupId=xxx -DartifactId=xxx -Dversion=0.0.1-SNAPSHOT-jar-with-dependencies -Dpackaging=jar

然后在我的java项目下的pom.xml导入这个依赖。如下:

<dependency>
		<groupId>xxx</groupId>
		<artifactId>xxx</artifactId>
		<version>0.0.1-SNAPSHOT-jar-with-dependency</version>
	</dependency>

这时候出问题了:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/xxx.jar/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/xxx.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

2 解决方案

1)先找原因,即先查看项目A的pom.xml看导入了什么日志依赖:

<!-- Logger -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.25</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.3</version>
		</dependency>

再查看我的java项目下的pom.xml导入了什么日志依赖:

<!-- For logging (remove <scope>test</scope>) -->
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.25</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>1.7.25</version>
	</dependency>

可以推断是我的项目和项目A的日志依赖冲突了。

2)不应该加入jar-with-dependencies这个jar包。即在项目A目录下运行mvn install,如下:

mvn clean package -DskipTests

mvn install 

3)然后在我的java项目下的pom.xml导入这个依赖(之前的0.0.1-SNAPSHOT-jar-with-dependency 依赖就不要了),加入exclusion项。如下:

<dependency>
		<groupId>xxx</groupId>
		<artifactId>xxx</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<exclusions>
			<!-- <exclusion>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
			</exclusion>	
	        <exclusion>
	            <groupId>ch.qos.logback</groupId>
	            <artifactId>logback-core</artifactId>
	        </exclusion>-->
	        <exclusion>
	            <groupId>ch.qos.logback</groupId>
	            <artifactId>logback-classic</artifactId>
	        </exclusion>
	    </exclusions>
	</dependency>

然后再编译我的项目的时候,就不会报错啦。

可以了。

小结

以上。

参考文献

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

or mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>

or mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=<path-to-file>

教你如何打包install maven项目。

在Linux上,如果遇到`SLF4J: Class path contains multiple SLF4J bindings`这样的错误,通常意味着你的项目中引入了多个日志框架,如logback和slf4j本身的jar包,它们试图竞争作为默认的日志管理器。解决这个问题需要确保只有一个SLF4J绑定被有效加载。这里有一些步骤可以帮助你: 1. **查看依赖**:首先,检查你的Maven或Gradle构建文件,确认是否有多个SLF4J相关的依赖项。移除多余的、未使用的库。 ```bash mvn dependency:tree | grep slf4j ``` 或者 ```bash gradle dependencies | grep slf4j ``` 2. **保留一个版本**:选择一个你喜欢的日志框架,例如slf4j本身或logback,并将其作为项目的主要日志库。删除其他版本。 3. **排除冲突**:在build.gradle或pom.xml文件中,对slf4j的依赖添加exclusions,明确指定排除其他冲突的版本。 例如,在Gradle中: ```groovy dependencies { implementation 'org.slf4j:slf4j-api:1.x.y' implementation 'org.slf4j:slf4j-log4j12:1.x.y' // 或者 logback-classic implementation 'ch.qos.logback:logback-core:1.x.y' implementation 'ch.qos.logback:logback-classic:1.x.y' exclude group: 'org.slf4j', module: 'slf4j-log4j12' // 或者exclude org.slf4j:slf4j-jdk14 } ``` 4. **配置桥接器**:如果你同时使用slf4j-api和logback-classic,可能需要在应用启动时添加一个BridgeHandler,确保两者能正常协同工作。比如在Spring Boot中,可以这样做: ```java import org.slf4j.bridge.SLF4JBridgeHandler; SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); ``` 5. **重启项目**:最后,清理并重新构建项目,然后重启应用程序以加载新的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值