日志框架不兼容

我正在构建一个小型的 Java 应用程序,希望能够使用 logback 进行日志记录。

我的应用程序有一个依赖于一个旧的项目,做它的日志通过

org.apache.commons | com.springsource.org.apache.commons.logging | 1.1.1

所以我的计划是

org.slf4j | jcl-over-slf4j | 1.5.6

将 JCL 的日志重定向到

org.slf4j | slf4j-api | 1.6.0

最终成为

ch.qos.logback | logback-classic | 0.9.22
ch.qos.logback | logback-core | 0.9.22

这样我的应用程序可以通过 slf4j API 通过 logback 登录,而旧的库代码可以通过重定向登录到相同的位置。

唉,这导致了

java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
at   org.apache.commons.logging.impl.SLF4JLocationAwareLog.info(SLF4JLocationAwareLog.java:141)

我已经尝试了一些罐子的版本号越来越高和越来越低,也挖掘了 API 文档等... 但我无法找到和解决这个问题。

帮帮我,好吗?

虽然 logback 被认为是“战略性”日志框架,但是我在最终使用日志机制方面还有一些余地。不过,我希望使用 logback 或 log4j,并且我肯定希望通过一个公共配置将旧项目的日志合并到“新”日志框架中。

109597 次浏览

You are mixing the 1.5.6 version of the jcl bridge with the 1.6.0 version of the slf4j-api; this won't work because of a few changes in 1.6.0. Use the same versions for both, i.e. 1.6.1 (the latest). I use the jcl-over-slf4j bridge all the time and it works fine.

Just to help those in a similar situation to myself...

This can be caused when a dependent library has accidentally bundled an old version of slf4j. In my case, it was tika-0.8. See https://issues.apache.org/jira/browse/TIKA-556

The workaround is exclude the component and then manually depends on the correct, or patched version.

EG.

<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>0.8</version>
<exclusions>
<exclusion>
<!-- NOTE: Version 4.2 has bundled slf4j -->
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- Patched version 4.2-min does not bundle slf4j -->
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.2-min</version>
</dependency>

SLF4J 1.5.11 and 1.6.0 versions are not compatible (see compatibility report) because the argument list of org.slf4j.spi.LocationAwareLogger.log method has been changed (added Object[] p5):

SLF4J 1.5.11:

LocationAwareLogger.log ( org.slf4j.Marker p1, String p2, int p3,
String p4, Throwable p5 )

SLF4J 1.6.0:

LocationAwareLogger.log ( org.slf4j.Marker p1, String p2, int p3,
String p4, Object[] p5, Throwable p6 )

See compatibility reports for other SLF4J versions on this page.

You can generate such reports by the japi-compliance-checker tool.

enter image description here