当前位置: 首页 > 知识库问答 >
问题:

SLF4J:类路径包含多个带maven的SLF4J绑定

钮高朗
2023-03-14

我已经在我的应用程序中使用-SLF4J创建了cutom日志,如下链接:,

http://javaeenotes.blogspot.com/2011/12/custom-slf4j-logger-adapter.html

它的工作与样本HelloWorldProgram这样很好,

public class HelloWorld {
    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(HelloWorld.class);
        logger.info("Hello World");
    }
}

但是如果我将它集成到maven项目中,使用spring创建日志文件,

我得到以下错误。尝试了许多不同的方法来解决这个问题,但运气不好。

11:08:59,486 ERROR [STDERR] SLF4J: Class path contains multiple SLF4J bindings.
11:08:59,486 ERROR [STDERR] SLF4J: Found binding in [/WEB-INF/classes/org/slf4j/impl/StaticLoggerBinder.class]
11:08:59,486 ERROR [STDERR] SLF4J: Found binding in [/WEB-INF/lib/logback-classic-1.0.13.jar/org/slf4j/impl/StaticLoggerBinder.class]
11:08:59,486 ERROR [STDERR] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
11:08:59,496 ERROR [STDERR] SLF4J: Actual binding is of type [org.slf4j.impl.MyLoggerFactory]
<dependencies>

    <!-- spring core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
        <scope>provided</scope>
    </dependency>

    <!-- logging dependencies -->
    <!-- <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency> -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.9</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions> 
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
        <exclusions>
            <exclusion>
                <!-- Defined below -->
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.0.13</version>
        <exclusions>
            <exclusion>
                <!-- Defined below -->
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <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>
            <exclusion> 
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
            </exclusion>
        </exclusions> 
    </dependency>    

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.owasp.esapi</groupId>
        <artifactId>esapi</artifactId>
        <version>2.1.0</version>
        <exclusions>
            <exclusion>
                <groupId>commons-configuration</groupId>
                <artifactId>commons-configuration</artifactId>
            </exclusion>
            <exclusion>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
            </exclusion>
            <exclusion>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>xom</groupId>
                <artifactId>xom</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.beanshell</groupId>
                <artifactId>bsh-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.owasp.antisamy</groupId>
                <artifactId>antisamy</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>                        
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <classpathDependencyExcludes>
                <classpathDependencyExcludes>ch.qos.logback:logback-classic</classpathDependencyExcludes>
            </classpathDependencyExcludes>
        </configuration>
        </plugin> 
    </plugins>
</build>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
</contextListener>


<appender name="appLogFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            //log file creation goes here
        </rollingPolicy>        
</appender>

在这个问题上有人能帮我吗?

共有1个答案

袁和通
2023-03-14

按照错误消息中给出的链接(http://www.slf4j.org/codes.html#multiple_bindings),SLF4J设计为“一次只能绑定一个底层日志框架”。

您的类路径上目前有两个:

  • 您自己的自定义活页夹(/WEB-INF/classes/org/slf4j/impl/StaticLoggerBinder.class)

当SLF4J发现多个绑定时,它只是随机选择一个并使用它。在您的案例中,它选择了您的自定义绑定(基于错误消息的最后一行“实际绑定的类型为[org.slf4j.impl.MyLoggerFactory]”),但并不保证每次都这样做。

您基本上有两种选择:

  • 如果您想使用自己的定制绑定器,那么应该从POM中删除logback依赖项
 类似资料: