Spring 5.0.2 中的 Log4jConfigurer

use*_*581 7 java spring log4j slf4j

我使用的是 Spring 4.XX 并使用以下设置来配置 Log4j。现在正在将它升级到 Spring 5.0.2,其中Log4jConfigurer该类已被删除。我如何在 Spring 5.0.2 中做到这一点?

<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
    <property name="targetMethod" value="initLogging"/>
    <property name="arguments">
        <list>
            <value>classpath:log4j.properties</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

gly*_*ing 7

Log4JConfigurer是非默认 Log4j 初始化所必需的,例如,如果使用自定义配置文件名/位置,但您的配置文件位于默认位置:classpath:log4j.properties因此您可以简单地删除Log4jConfigurer声明,Spring 将自动发现您的log4j.properties.

这里有一个可能的警告;Spring 5 使用 Log4j v2(遵循 Apache 对 log4j 1.x 的 EOL 声明),因此只要您使用 Log4j v2,Spring 5 就会自动检测它和您的log4j.properties文件,而无需声明Log4JConfigurer. 如果您当前没有使用 Log4j v2,那么我认为您需要升级,因为 Spring 5 不支持使用 Log4j v1.x。