log4j-1.2-api 桥 ClassNotFoundException:org.apache.log4j.RollingFileAppender

log4j-1.2-api bridge ClassNotFoundException:org.apache.log4j.RollingFileAppender

我尝试将我的应用程序从 log4j 迁移到 log4j2。根据 official manual,我的 Maven 依赖项中有 org.apache.logging.log4j:log4j-1.2-api:2.17.1org.apache.logging.log4j:log4j-api:2.17.1org.apache.logging.log4j:log4j-core:2.17.1。我使用 log4j.properties 文件,所以我启用了兼容模式 (-Dlog4j1.compatibility=true)。 RollingFileAppender 在我的 log4j.properties 文件中配置:

log4j.appender.A2=org.apache.log4j.RollingFileAppender

我收到以下错误:

2022-02-16 21:04:20,396 main ERROR Unable to create Appender org.apache.log4j.RollingFileAppender due to ClassNotFoundException:org.apache.log4j.RollingFileAppender

根据 official manualRollingFileAppender 是受支持的组件。在迁移到 log4j2 之前,我有 log4j:log4j:1.2.17 依赖项,它包含 org.apache.log4j.RollingFileAppender class。不幸的是,log4j-1.2-apilog4j-apilog4j-core 库中的 none 包含此 class。我不明白如果手册说 RollingFileAppender 是受支持的组件,为什么 RollingFileAppender 不存在于 log4j 桥中。我找到了类似的问题,但没有满意的答案。如何迁移 RollingFileAppender?

更新

这是我的 log4j.properties 文件:

log4j.rootCategory=INFO, A1, A2

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A2=org.apache.log4j.RollingFileAppender

log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d%m%M %n

log4j.appender.A2.file=C:/projects/test/Log.log
#log4j.appender.A2.layout=org.apache.log4j.PatternLayout
#log4j.appender.A2.layout.ConversionPattern=[%d{dd.MM HH.mm.ss.SSS} %-5p %-20c{1}] : %m%n
#log4j.appender.A2.MaxFileSize=10000KB
#log4j.appender.A2.MaxBackupIndex=3

我加了-Dlog4j2.debug=true。有大量的日志。我选择了有趣的日志:

TRACE StatusLogger Trying to find [log4j.properties] using context class loader jdk.internal.loader.ClassLoaders$AppClassLoader@2437c6dc.
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger PluginManager 'Log4j Builder' found 18 plugins
DEBUG StatusLogger Parsing for [root] with value=[INFO, A1, A2].
DEBUG StatusLogger Level token is [INFO].
DEBUG StatusLogger Logger root level set to INFO
DEBUG StatusLogger Parsing appender named "A1".
DEBUG StatusLogger PluginManager 'Converter' found 47 plugins
DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false
DEBUG StatusLogger Adding appender named [A1] to loggerConfig [].
DEBUG StatusLogger Parsing appender named "A2".
WARN StatusLogger Unable to create File Appender, no file name provided
ERROR StatusLogger Unable to create Appender org.apache.log4j.RollingFileAppender due to ClassNotFoundException:org.apache.log4j.RollingFileAppender
DEBUG StatusLogger Appender named [A2] not found.
DEBUG StatusLogger Finished configuring.

配置失败的原因由以下消息解释:

WARN StatusLogger Unable to create File Appender, no file name provided

这是由 Log4j 1.x 桥和原始 Log4j 1.x 之间的兼容性问题引起的:属性 名称必须以大写字母开头(参见 LOG4J2-3316).这将在下一个版本中解决。

到那时,您可以使用:

log4j.appender.A2.File=C:/projects/test/Log.log

(大写 F)。