【问题标题】:Log4jConfigurer in Spring 5.0.2Spring 5.0.2 中的 Log4jConfigurer
【发布时间】:2018-06-14 21:47:27
【问题描述】:

我使用的是 Spring 4.X.X 并使用以下设置来配置 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>

【问题讨论】:

    标签: java spring log4j slf4j


    【解决方案1】:

    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。

    【讨论】:

    • 感谢 Glytching,我目前正在使用 log4j v1.x,现在升级到 v2.10.0。我也使用 org.apache.log4j.ext.SNMPTrapAppender v1.2.8 这个是否也与 Spring v5..0.2 不兼容?我无法找到 org.apache.log4j.ext.SNMPTrapAppender 的 V2.x。
    • log4j.appender.TRAP_LOG=org.apache.log4j.ext.SNMPTrapAppender log4j.appender.TRAP_LOG.ImplementationClassName=org.apache.log4j.ext.JoeSNMPTrapSender
    • 我不知道 SNMPTrapAppender 的 Log4j2 版本。
    • 如果要将 log4j.properties 文件放在自定义位置,那么如何提供该路径到 Spring v5 进行初始化。
    • 参见the docs,具体来说:“Log4j 将检查“log4j.configurationFile”系统属性...”。
    猜你喜欢
    • 2012-08-29
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多