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

Kon*_*ner 14 binding warnings suppress slf4j maven

我的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.
Run Code Online (Sandbox Code Playgroud)

PS:这与slf4j警告关于相同绑定重复的问题不一样,答案是如何摆脱虚警警告,但在我的情况下,这是一个真正的警告.PSS:对不起,我忘了提一下:我使用Maven和SLF4J包含在我的依赖项的依赖项中.

pal*_*int 20

从类路径中删除其中一个slf4j-log4j12-1.5.8.jarslf4j-log4j12-1.6.0.jar.您的项目不应该依赖于不同版本的SLF4J.我建议你只使用1.6.0.

如果您正在使用Maven,则可以排除传递依赖项.这是一个例子:

<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>
Run Code Online (Sandbox Code Playgroud)

使用当前的slf4j-api实现,无法删除这些警告.该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.");
  }
  ...
Run Code Online (Sandbox Code Playgroud)

Util课程如下:

public class Util {

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

report方法直接写入System.err.解决方法可能是第一次调用之前替换System.errwith ,但如果这样做,您可能会丢失其他重要消息.System.setErr() LoggerFactory.getLogger()

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


ugo*_*ugo 6

    PrintStream filterOut = new PrintStream(System.err) {
        public void println(String l) {
            if (! l.startsWith("SLF4J")) {
                super.println(l);
            }
        }
    };
    System.setErr(filterOut);
Run Code Online (Sandbox Code Playgroud)

等等!


Cek*_*eki 5

您是否阅读过警告引用的URL?

SLF4J: See [http://www.slf4j.org/codes.html#multiple_bindings][1] for an explanation.
Run Code Online (Sandbox Code Playgroud)

以下链接指出:

SLF4J API被设计为一次只与一个底层日志框架绑定.如果类路径上存在多个绑定,SLF4J将发出警告,列出这些绑定的位置.发生这种情况时,选择您希望使用的唯一一个绑定,并删除其他绑定.

例如,如果类路径上同时包含slf4j-simple-1.6.2.jar和slf4j-nop-1.6.2.jar,并且希望使用nop(无操作)绑定,则删除slf4j-simple- 1.6.2.jar来自班级路径.

请注意,SLF4J发出的警告只是一个警告.SLF4J仍将与它在类路径上找到的第一个框架绑定.