Class path contains multiple SLF4J bindings解决
阅读原文时间:2023年07月08日阅读:4

1.根据控制台查看冲突的日志依赖

本工程Maven依赖

    <dependencies>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.28</version>
        </dependency>

        <!--  web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

根据控制台查看,为logback和log4j冲突

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/yikolemon/.m2/repository/org/slf4j/slf4j-log4j12/1.7.28/slf4j-log4j12-1.7.28.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/yikolemon/.m2/repository/ch/qos/logback/logback-classic/1.2.9/logback-classic-1.2.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]

2.通过Maven依赖分析

Maven右击Dependencies,点击Analyze dependencies

搜索logback,查看其所在的父依赖为org.springframework.boot:spring-boot-starter-logging:2.6.2

3.排除依赖

排除掉多余的依赖,重新测试正常(如果不注册,需要查看日志框架的配置)

<dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.28</version>
        </dependency>

        <!--  web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>