简体   繁体   中英

How to use SLF4J when there is multiple SLF4J bindings

When I add the SLF4J logger into my code, I get an error saying, "Class path contains multiple SLF4J bindings." On the website slf4j.org/codes it states that I should remove them from the class path. However, these two loggers are included in my maven dependencies. And my whole maven dependency folder is included into the class path. I'm not in charge of what goes into the maven dependencies, so it's not my place to edit it so that it only has one logger dependency inside the maven dependency folder. Can I specify the Java Program so that it only uses one of the loggers instead?

ejay

If you're certain you can't adjust the existing maven dependencies to fix the problem, you could make sure your SLF4J binding appears in the classpath first, as the first binding is the one that gets used in the case of multiple bindings.

You'll still get the warning however, but your SLF4J logger will be the one that gets used.

You could also consider utilising maven modules to split the project up into sections so that you can manage the dependencies in each section differently in each module's pom.xml file.

Figure out which of your project's dependencies is including an slf4j implementation, then exclude it:

    <dependency>
        <groupId>other-group</groupId>
        <artifactId>dependency-id</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                                    <!-- or slf4j-jdk14, etc -->
                <artifactId>slf4j-simple</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM