【问题标题】:Maven SL4J multiple bindings, previous solutions failMaven SL4J 多重绑定,之前的解决方案失败
【发布时间】:2018-11-04 20:20:43
【问题描述】:

我有一个具有多个 SLF4J 绑定的项目。 我已阅读并尝试过this SO postthis other SO post 中的解决方案 和slf4j website

如果我在运行代码时看到的是

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/Mike/.m2/repository/org/jlab/coat/coat-libs/5.1-SNAPSHOT/coat-libs-5.1-SNAPSHOT.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/Mike/.m2/repository/org/slf4j/slf4j-log4j12/1.7.16/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]

但是在我的 pom.xml 文件中,我已经有了

    <dependency>
        <groupId>org.jlab.coat</groupId>
        <artifactId>coat-libs</artifactId>
        <version>5.1-SNAPSHOT</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

并且在 mvn 依赖项中:tree 我没有看到排除 jar 的此依赖项,仅适用于 Spark jar,即

[INFO] com.IKP:DCdatabase:jar:0.0.1-SNAPSHOT
[INFO] +- org.jlab.coat:coat-libs:jar:5.1-SNAPSHOT:compile
[INFO] +- org.apache.spark:spark-core_2.11:jar:2.2.1:compile
...
...
...
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] |  +- log4j:log4j:jar:1.2.17:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.16:compile

我还尝试了更多步骤,例如清理我的 .m2 目录,并从头开始构建所有源,但我仍然看到这种双重绑定。

发生的细微差别是 Spark 的日志抑制确实发生了,即

    Logger.getLogger("org.apache.spark.SparkContext").setLevel(Level.WARN);
    Logger.getLogger("org").setLevel(Level.OFF);
    Logger.getLogger("akka").setLevel(Level.OFF);

不再将关卡设置为关闭,我可以看到所有关卡。

还有其他方法可以删除 SLF4J 的多重绑定吗?

【问题讨论】:

    标签: apache-spark maven-2 slf4j


    【解决方案1】:

    在我看来,coat-libs 已被打包为 uber jar,这就是为什么当您执行 mvn dependency:tree 时 slf4j 不会显示为依赖项的原因。这解释了为什么您的排除不起作用。

    我建议您查看用于打包 jar 的 maven shade 插件。然后,您可以使用过滤器从 coat-libs using a filter 中排除 slf4j 依赖项。

    例如,可能是这样的:

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <filters>
                    <filter>
                      <artifact>org.jlab.coat:coat-libs</artifact>
                      <excludes>
                        <exclude>org/slf4j/**</exclude>
                      </excludes>
                    </filter>
                    <filter>
                      <artifact>*:*</artifact>
                      <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                        <exclude>META-INF/*.RSA</exclude>
                      </excludes>
                    </filter>
                  </filters>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    

    【讨论】:

    • 我接受这个答案,因为多重绑定语句不再出现。但是由于某种原因,这仍然没有解决记录器除了“警告”之外的所有内容
    【解决方案2】:

    正如消息所说,您从coat-libs-5.1-SNAPSHOT.jar 获得一个绑定,在slf4j-log4j12-1.7.16.jar 获得另一个绑定。这并不是说“coat-libs”试图引入一个具有绑定的依赖项,它一个试图处理 SLF4J 日志记录的日志记录绑定。您只能使用一个日志绑定,因此您需要删除外套库的使用,或者您需要从 spark-core 的依赖项中排除 slf4j-log4j12,具体取决于您实际尝试使用的日志框架。

    【讨论】:

    • 感谢您的回答,但正如我的标题所说,您排除一个绑定的方法不起作用。例如,如果我从 spark-core 中排除绑定,那么 spark 不知道从哪里获取绑定。如果我尝试从 class 的 uber jar 中排除,则不会排除任何内容。
    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 2013-01-06
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多