如何抑制有关多个绑定的 SLF4J 警告?

2024-01-20

我的 java 项目与不同的 SLF4J 版本有依赖关系。如何抑制烦人的警告?

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:xyz234/lib/slf4j-
log4j12-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:xyz123/.m2/repository/org/slf4j/slf4j-log4j12
/1.6.0/slf4j-log4j12-1.6.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

P.S.:这不是同一个问题slf4j 关于相同绑定重复的警告 https://stackoverflow.com/questions/6418147/slf4j-warning-about-the-same-binding-being-duplicate,答案是如何摆脱误报警告,但就我而言,这是一个真实的警告。 P.S.S.:抱歉,我忘了提及:我使用 Maven,并且 SLF4J 包含在我的依赖项的依赖项中。


删除其中一项slf4j-log4j12-1.5.8.jar or slf4j-log4j12-1.6.0.jar来自类路径。您的项目不应依赖于不同版本的 SLF4J。我建议您只使用 1.6.0。

如果您使用 Maven,您可以排除传递依赖 http://maven.apache.org/pom.html#Exclusions。这是一个例子:

<dependency>
    <groupId>com.sun.xml.stream</groupId>
    <artifactId>sjsxp</artifactId>
    <version>1.0.1</version>
    <exclusions>
        <exclusion>
            <groupId>javax.xml.stream</groupId>
            <artifactId>stax-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

随着当前 slf4j-api 实现 http://search.maven.org/#artifactdetails%7Corg.slf4j%7Cslf4j-api%7C1.6.2%7Cjar无法删除这些警告。这org.slf4j.LoggerFactory类打印消息:

  ...
  if (implementationSet.size() > 1) {
    Util.report("Class path contains multiple SLF4J bindings.");
    Iterator iterator = implementationSet.iterator();
    while(iterator.hasNext()) {
      URL path = (URL) iterator.next();
      Util.report("Found binding in [" + path + "]");
    }
    Util.report("See " + MULTIPLE_BINDINGS_URL + " for an explanation.");
  }
  ...

The Util类如下:

public class Util {

  static final public void report(String msg, Throwable t) {
    System.err.println(msg);
    System.err.println("Reported exception:");
    t.printStackTrace();
  }
  ...

The report方法直接写入System.err。解决方法可能是替换System.err with System.setErr() http://download.oracle.com/javase/6/docs/api/java/lang/System.html#setErr%28java.io.PrintStream%29 before首先LoggerFactory.getLogger()拨打电话,但如果这样做,您可能会丢失其他重要消息。

当然你可以下载源码并删除这些Util.report调用并在项目中使用修改后的 slf4j-api。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何抑制有关多个绑定的 SLF4J 警告? 的相关文章

随机推荐