You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jersey Servlet容器中使用slf4j-api 2.x版本

SLF4J 2.0.x 升级问题:Tomcat + Jersey + Maven项目日志无法输出

项目背景

  • 基于Jersey Servlet构建,使用Maven管理依赖,部署运行在Tomcat中
  • 尝试将slf4j-api升级至2.0.17版本,遇到日志后端适配失败问题

SLF4J 2.0.x 核心变化(官方说明)

最显著的变化是,slf4j-api现在依赖ServiceLoader机制来查找日志后端。SLF4J 1.7.x及更早版本依赖的静态绑定机制不再被slf4j-api 2.0.x版本支持。具体来说,LoggerFactory类初始化时将不再在类路径中搜索org.slf4j.impl.StaticLoggerBinder类。
现在org.slf4j.LoggerFactory不再寻找“绑定”,而是寻找“提供者”。例如slf4j-nop-2.0.x.jar、slf4j-simple-2.0.x.jar或slf4j-jdk14-2.0.x.jar中就包含这类提供者。

尝试的依赖与问题

原1.7.x版本依赖(不适配2.0.x)

  • log4j-core
  • log4j-web
  • log4j-slf4j-impl

依赖log4j-slf4j2-impl:2.25.1时的问题

启动时触发警告,日志完全无法输出:

SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation

依赖log4j-slf4j-impl:2.25.1时的问题

启动时触发以下警告,日志同样无法输出:

SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J(W): Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J(W): Ignoring binding found at [jar:file:/path/to/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/quality-check/WEB-INF/lib/log4j-slf4j-impl-2.25.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]

其中无法理解的提示:Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier

日志使用方式

在需要日志的类中,通过以下代码获取Logger实例:

org.slf4j.LoggerFactory.getLogger(MyClass.class);

需求

寻求适配SLF4J 2.0.x的正确依赖配置方案,目前暂时回退到1.7.x版本,但希望完成版本升级。

内容的提问来源于stack exchange,提问作者BairDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 10:52:36