与 slf4j 的绑定错误

Binding error with slf4j

我正在使用 GATE 工具进行文本分析,在安装 PR 时出现以下错误-

SLF4J:您的 slf4j 绑定请求的版本 1.5.6 与 [1.6、1.7] 不兼容

现在,我已经在互联网上搜索了这个,并找到了这个-

 Mixing different versions of slf4j-api.jar and SLF4J binding can cause problems. For example, if you are using slf4j-api-1.7.2.jar, then you should also use slf4j-simple-1.7.2.jar, using slf4j-simple-1.5.5.jar will not work.

我正在使用 slf4j-api-1.7.12 ,但是我的计算机上不存在这个文件- slf4j-simple-N 。 我该如何解决这个问题? 任何帮助将不胜感激。谢谢!

SLF4J 是 "wrap" "real" 日志框架的标准化方式。 ("F" 代表 "Facade"。)除了 slf4j-api-<version>.jar 文件外,还需要包含一个“binding”。 slf4j-simple-<version>.jar 是一种非常简单的应用程序绑定,它有一些简单的配置,主要用于记录到控制台。但是,如果您的类路径中没有它,那么它可能不是您正在使用的那个。

那么,您使用的是哪种绑定?如果您不确定,弄清楚这一点需要您查看类路径上的 .jar 文件,看看哪些可以绑定 SLF4J。绑定库通常以 "slf4j-" 开头,但有些像 "logback" 不直接实现 SLF4J API。如果你真的卡住了,你可能需要查看 .jar 文件(它们可以用任何 .zip 文件读取 reader)并查看其中有一个 org/slf4j 文件夹。

我认为您真正的问题出在您使用的任何依赖管理系统中,例如 Maven、Gradle,或者使用一堆 .jar 文件手动构建类路径。看起来您为此使用的任何东西都具有 slf4j-api 文件的多个版本、多个日志记录绑定,或者 slf4j-api 的旧版本与您绑定库的新版本不匹配正在使用。查看您所在的类路径中的所有 .jar 文件 运行,您应该能够发现问题。

编辑:问题在 GATE 8.4 中不再存在,其中 classGATE 中的加载确实允许每个插件是独立的,因此库由一个插件加载不要干扰其他插件加载的那些。


问题

我想,我可以重现这个问题。 它在 GATE Developer 中出现,当加载两个插件并且每个插件都使用不同版本的 slf4j-api 时。例如 Ontology 插件使用 slf4j 1.5.6Stanford_CoreNLP slf4j 1.7.12.

当尝试创建 Stanford POS Tagger 的新实例时,它以以下错误消息结束(请参阅下面的完整日志):

java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl...

GATE 8.2 build 5482 started at Mon Jul 04 21:54:09 CEST 2016
and using Java 1.8.0_91 Oracle Corporation on Windows 8.1 amd64 6.3.
CREOLE plugin loaded: file:/C:/Program%20Files/gate-8.2-build5482-BIN/plugins/Stanford_CoreNLP/
CREOLE plugin loaded: file:/C:/Program%20Files/gate-8.2-build5482-BIN/plugins/Ontology/
org.xml.sax.helpers.DefaultHandler is available via both the system classpath and a plugin; the plugin classes will be ignored
SLF4J: The requested version 1.5.6 by your slf4j binding is not compatible with [1.6, 1.7]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of gate/util/GateClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of gate/util/GateClassLoader) for the method's defining class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:335)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:283)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:304)
    at edu.stanford.nlp.io.IOUtils.<clinit>(IOUtils.java:42)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:765)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:298)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:263)
    at gate.stanford.Tagger.init(Tagger.java:129)
    at gate.Factory.createResource(Factory.java:432)
    at gate.gui.NewResourceDialog.run(NewResourceDialog.java:270)
    at java.lang.Thread.run(Unknown Source)

解决方案

我能想到三种可能的解决方案:

1) 去除不必要的插件

你真的需要这两个插件与不兼容的 slf4j 版本吗?如果没有,只需卸载不需要的插件(一定要重新启动 GATE),问题就会消失。

2) 防止加载 ONE 插件的 slf4j jar

这个解决方案比下一个更脏(因为修改后的插件不能单独工作)但它应该足以作为快速修复。选择其中一个插件并从插件的 creole.xml 文件中删除 slf4j 条目。同样,问题应该在 GATE 重新启动后消失。

这对于 Ontology 插件来说非常简单:(注意注释掉的行)

<?xml version="1.0"?>
<CREOLE-DIRECTORY> 
      <JAR>lib/commons-httpclient-3.1.jar</JAR>
      <JAR>lib/owlim-lite-5.4.jar</JAR>
<!--  <JAR>lib/slf4j-api-1.5.6.jar</JAR>
      <JAR>lib/slf4j-jdk14-1.5.6.jar</JAR> -->
      <JAR>lib/openrdf-sesame-2.7.9-onejar.jar</JAR>
      <JAR SCAN="true">Ontology.jar</JAR>
</CREOLE-DIRECTORY>

对于 Stanford_CoreNLP 插件,它更复杂,因为它使用 Apache Ivy 加载 slf4j jar,并且它们有要在 ivy.xml 文件中排除(plugins\Stanford_CoreNLP\build\ivy.xml,请注意文件底部添加的行 <exclude org="org.slf4j"/>

<ivy-module version="2.0">

  <info
    organisation="uk.ac.gate.plugins"
    module="stanford_corenlp"
    revision="8.2-SNAPSHOT">
    <description homepage="https://github.com/GateNLP/gateplugin-Stanford_CoreNLP/" />
  </info>

  <configurations>
    <conf name="default" />
  </configurations>

  <dependencies defaultconf="*->master(default),runtime(default)" >
    <dependency org="edu.stanford.nlp" name="stanford-corenlp" rev="3.6.0" />
    <exclude org="org.slf4j"/>
  </dependencies>
</ivy-module>

3) 统一冲突的slf4j版本

这似乎是一个干净的解决方案,但它比我预期的要复杂得多,因为即使两个插件使用相同版本的 slf4j,问题仍然存在。问题可能更深层次地存在于 GATE class-加载机制中,唯一 "working for me way" 如何统一 slf4j 版本是在所有冲突的插件中排除它们并添加 slf4j jar(例如来自Ontology 插件)到 GATE 的 lib 文件夹。