使用logback/slf4j时抛出异常

hai*_*der 18 java logback slf4j

我正在使用slf4j 1.6.2 api jar(也尝试使用1.6.1) - logback版本是0.9.29(核心和经典).我在ubuntu上使用jdk1.6.我收到的例外情况将在下面复制.

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
    at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:112)
    at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:471)
    at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:427)
    at ch.qos.logback.classic.Logger.info(Logger.java:631)
Run Code Online (Sandbox Code Playgroud)

我也收到一条抱怨slf4j绑定不匹配的消息.

"SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.10, 1.5.11]"
Run Code Online (Sandbox Code Playgroud)

Cek*_*eki 25

看起来像JVM加载的slf4j-api.jar的版本有1.5.x版本.你的类路径上肯定有slf4j-api-1.5.x.jar(除了slf4j-api-1.6.2.jar).检查你的课程路径.

  • 大多数情况下,它来自依赖项的依赖项,...使用`mvn dependency:tree`来检查依赖项 (8认同)

Tri*_*jee 9

添加以下依赖项可能会有所帮助:

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jul-to-slf4j</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>log4j-over-slf4j</artifactId>
      <version>1.7.7</version>
  </dependency>

  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>1.7.7</version>
  </dependency>
Run Code Online (Sandbox Code Playgroud)


itr*_*tro 8

slf4j-api版本与绑定版本不匹配:

SLF4J绑定指定一个工件,例如slf4j-jdk14.jar或slf4j-log4j12.jar,用于将slf4j绑定到底层日志框架,例如java.util.logging和log4j.

混合不同版本的slf4j-api.jar和SLF4J绑定可能会导致问题.例如,如果您使用的是slf4j-api-1.7.2.jar,那么您还应该使用slf4j-simple-1.7.2.jar,使用slf4j-simple-1.5.5.jar将无效.

注意从客户端的角度来看,所有版本的slf4j-api都是兼容的.对于任何N和M,使用slf4j-api-N.jar编译的客户端代码与slf4j-api-M.jar完全匹配.您只需要确保绑定的版本与slf4j-api.jar的版本匹配.您不必担心项目中给定依赖项使用的slf4j-api.jar的版本.你可以随时使用任何版本的slf4j-api.jar,只要slf4j-api.jar的版本及其绑定匹配,你应该没问题.

在初始化时,如果SLF4J怀疑可能存在api与绑定版本不匹配问题,则会发出有关可疑不匹配的警告.

来自http://www.slf4j.org,我希望它可以提供帮助.