【问题标题】:Does slf4j use commons-logging implementation by default?slf4j 默认使用 commons-logging 实现吗?
【发布时间】:2013-02-21 23:10:38
【问题描述】:

我过去只与log4j 合作过。现在我正在寻找一个新项目,并注意到它使用了slf4j 1.7.2。我知道它只是一个 API 规范,它为符合它的各种实现提供简化的接口(AKA 外观),例如java.util.logging, log4j and logback。但是,我注意到 API 网站的列表中没有提到 commons-logging,但是,它的 jar 在这个应用程序的类路径中。当我从类路径中删除它以检查它是否是使用的实现时,我确认它确实是:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

commons-logging 确实是 slf4j 的默认实现吗?因为他们的文档说:

如果在类路径上没有找到绑定,那么 SL​​F4J 将默认为无操作实现。

在上面的链接页面。即使我在类路径中有slf4j-nop-1.7.2.jar,我猜上面提到的默认实现是,如果公共不在类路径中,我仍然会收到异常要求org/apache/commons/logging/LogFactory,所以我很困惑。

我们将不胜感激。

【问题讨论】:

  • 我的猜测是您拥有的其他一些依赖项正在拉入公共日志记录。如果您使用 maven 或类似的构建工具,您应该能够可视化您的依赖关系树并查看包含 commons-logging 的内容。
  • 其实我用的是Ant

标签: java logging slf4j apache-commons-logging


【解决方案1】:

您需要在类路径上组合绑定 jar 和日志记录实现(在本例中为 JCL)。

Binding with a logging framework at deployment time

commons-logging 不是默认的,但如果你的类路径中有 slf4j-jcl-1.7.2.jar,它会尝试使用它。因此,如果您因此从路径中删除 JCL,它会抱怨找不到类。

slf4j-nop-1.7.2.jar 是无操作的“SLF4J 绑定”jar,本身不是实现。

【讨论】:

  • 很遗憾,当我添加slf4j-jcl-1.7.2.jar时,它仍然抱怨找不到公共资源
  • 我在类路径中的内容是:slf4j-api-1.7.2.jarslf4j-jcl-1.7.2.jar
  • 但是你的类路径中有commons-logging-1.1.1.jar吗? slf4j-jcl-1.7.2.jar 告诉 SLF4J 使用 commons-logging,但如果您的路径中没有实际的 commons-logging jar,它将无法工作。
猜你喜欢
  • 2019-05-12
  • 2011-08-24
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
相关资源
最近更新 更多