Log4j 加载定义的配置,稍后停止并使用 spring boot 的 log4j.xml

问题描述 投票:0回答:1

我最近将一个遗留的 spring boot 项目从 Spring boot 版本 1.5.9.RELEASE 迁移到 2.7.7

迁移后,除了 log4j 初始化之外,我发现一切正常。我在项目中使用的是最新的 log4j 版本(2.20.0)

在应用程序启动期间,我看到我的 log4j 配置已加载,稍后停止,它使用 spring boot jar 的 log4j2.xml

DEBUG StatusLogger Reconfiguration complete for context[name=18b4aac2] at URI /home/vboxuser/Desktop/cande2/cande2-config/log4j2.xml (org.apache.logging.log4j.core.LoggerContext@7ff2a664) with optional ClassLoader: null
DEBUG StatusLogger Shutdown hook enabled. Registering a new one.
DEBUG StatusLogger LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@7ff2a664] started OK.
DEBUG StatusLogger uri does not represent a local file: jar:file:/home/vboxuser/.m2/repository/org/springframework/boot/spring-boot/2.7.7/spring-boot-2.7.7.jar!/org/springframework/boot/logging/log4j2/log4j2.xml
DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@57175e74
DEBUG StatusLogger PluginManager 'Lookup' found 16 plugins
DEBUG StatusLogger Closing JarURLInputStream sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@16afbd92
DEBUG StatusLogger Loaded configuration from jar:file:/home/vboxuser/.m2/repository/org/springframework/boot/spring-boot/2.7.7/spring-boot-2.7.7.jar!/org/springframework/boot/logging/log4j2/log4j2.xml

这让我无法使用自己的 log4j 配置。它还会在 log4j 初始化后在日志中抛出以下错误。

2023-03-16 19:57:21.802  INFO 11942 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1243 ms
ERROR StatusLogger Recursive call to appender Console
ERROR StatusLogger Recursive call to appender Console
ERROR StatusLogger Recursive call to appender Console

让我知道你对这个问题的看法。

我检查并确认没有传递性 log4j 依赖项进来。

java spring-boot log4j log4j2 springfox
1个回答
0
投票

最后,在 herehere 阅读了 spring 文档之后。我知道如果我们不将 log4j 或 logback 配置文件放在类路径上,我们可以通过在 application.properties/application.yml 中放置

logging.config
属性或使用 JVM 参数让 springboot 知道配置文件的位置
-Dlogging.config=/<YOUR_CONFIG_FOLDERPATH>/log4j2.xml
。这仅适用于 Springboot 应用程序。

对于不使用 Springboot 的应用程序(例如:Spark Java 应用程序),我们仍然可以使用 VM 参数

-Dlog4j.configurationFile=/<YOUR_CONFIG_FOLDERPATH>/log4j2.xml
(用于 log4j/log4j2)和
-Dlogback.configurationFile=/<YOUR_CONFIG_FOLDERPATH>/logback.xml
(用于 logback)

© www.soinside.com 2019 - 2024. All rights reserved.