[英]How to use log4j.xml for spring boot + log4j2 dependency
I have a log4j.xml
with a customized appender like:我有一个带有自定义附加程序的log4j.xml
,例如:
<appender name="console" class="com.example.MyAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m (%c{1}:%L)"/>
</layout>
</appender>
Recently I upgraded log4j
dependency to log4j2
, but still using this log4j.xml
and it works.最近我将log4j
依赖升级到log4j2
,但仍然使用这个log4j.xml
并且它有效。
Now, I add a Spring Boot module in my project.现在,我在我的项目中添加了一个 Spring 引导模块。 Following Spring doc , I set my pom.xml
as在Spring doc之后,我将pom.xml
设置为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.6.4</version>
</dependency>
And I also add arguments -Dlogging.config=log4j.xml -Dlog4j.configuration=log4j.xml -Dlog4j1.compatibility=true
for it.我还为此添加了 arguments -Dlogging.config=log4j.xml -Dlog4j.configuration=log4j.xml -Dlog4j1.compatibility=true
。
But my Spring application shows the error and no log output:但是我的 Spring 应用程序显示错误并且没有日志 output:
ERROR StatusLogger Unknown object "logger" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored: try nesting it inside one of: ["Appenders", "Loggers", "Properties", "Scripts", "CustomLevels"].错误 StatusLogger Unknown object org.apache.logging.log4j.core.config.LoggerConfig 类型的“记录器”被忽略:尝试将其嵌套在以下之一中:[“Appenders”、“Loggers”、“Properties”、“Scripts”、“自定义级别”]。
Seems log4j2
lib cannot recognize log4j.xml
, which means -Dlog4j1.compatibility=true
does not work for Spring Boot I think.似乎log4j2
lib 无法识别log4j.xml
,这意味着-Dlog4j1.compatibility=true
我认为不适用于 Spring Boot。
Any related config can be utilized or any workaround?可以使用任何相关配置或任何解决方法吗? Thanks.谢谢。
TL;DR: The problem is that Log4j2 has two XML configuration factories (for the Log4j 1.x and Log4j 2.x formats), with the 2.x format having higher priority. TL;DR:问题是 Log4j2 有两个 XML 配置工厂(用于 Log4j 1.x 和 Log4j 2.x 格式),2.x 格式具有更高的优先级。 You need to explicitly set the ConfigurationFactory
to use:您需要显式设置ConfigurationFactory
以使用:
-Dlog4j2.configurationFactory=org.apache.log4j.xml.XmlConfigurationFactory
When a Spring Boot application starts Log4j2 is configured twice:当一个 Spring Boot 应用程序启动时,Log4j2 配置了两次:
at the very beginning using Log4j2 automatic configuration .一开始使用Log4j2自动配置。 For this round you just need to set -Dlog4j1.compatibility=true
and call the config file log4j.xml
or call the file differently and set -Dlog4j.configuration
.对于这一轮,您只需要设置-Dlog4j1.compatibility=true
并调用配置文件log4j.xml
或以不同方式调用文件并设置-Dlog4j.configuration
。
when Spring's environment is ready, Spring reconfigures Log4j2 programmatically using only a subset of Log4j2 automatic configuration.当 Spring 的环境准备就绪时,Spring 仅使用 Log4j2 自动配置的一个子集以编程方式重新配置 Log4j2。 That is why this phase requires many manual settings:这就是为什么这个阶段需要很多手动设置:
-Dlogging.config=log4j.xml
: Spring does not look for a file named log4j.xml
, -Dlogging.config=log4j.xml
:Spring 不查找名为log4j.xml
的文件,-Dlog4j1.compatibility=true
to activate the Log4j 1.x configuration factories, -Dlog4j1.compatibility=true
激活 Log4j 1.x 配置工厂,-Dlog4j2.configurationFactory=org.apache.log4j.xml.XmlConfigurationFactory
to increase the priority of the Log4j 1.x XML configuration factory. -Dlog4j2.configurationFactory=org.apache.log4j.xml.XmlConfigurationFactory
增加 Log4j 1.x XML 配置工厂的优先级。Remark : Using a native Log4j 1.x custom appender exposes you to all the problems (synchronization and performance) of the original Log4j 1.x.备注:使用本机 Log4j 1.x 自定义附加程序会使您面临原始 Log4j 1.x 的所有问题(同步和性能)。 For example Log4j 1.x looses events during reconfiguration (as the one performed by Spring Boot), whereas Log4j 2.x does not.例如 Log4j 1.x 在重新配置期间丢失事件(如 Spring Boot 执行的事件),而 Log4j 2.x 则不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.