本文介绍了在Spring中正确使用Log4jConfigurer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们决定将log4j配置文件命名为自定义名称,以避免无意中从另一个jar加载默认文件.要进行配置,我们使用 org.springframework.util.Log4jConfigurer 指定log4j位置.

In our application, we decided to name the log4j configuration file as a custom name to avoid inadvertent loading of the default file from another jar. To configure this, we use org.springframework.util.Log4jConfigurer to specify the log4j location.

<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass">
        <value>org.springframework.util.Log4jConfigurer</value>
    </property>
    <property name="targetMethod">
        <value>initLogging</value>
    </property>
    <property name="arguments">
        <list>
            <value>classpath:com/kilo/custom-log4j.xml</value>
        </list>
    </property>
</bean>

这还有助于将所有配置保留在代码中,让新开发人员开始运作(而不是将其保存在容器的 setenv.sh 中,而不是将其保存在测试用例中)).到目前为止,我们非常高兴,直到我们发现由于这个原因而错过了Spring容器本身中的一些有价值的日志记录.

This also helps to keep all of the configuration in the code and let's a new developer hit the ground running (rather than keeping it in some setenv.sh for the container and separately for the test cases). So far we were sufficiently happy till we discovered that some valuable logging from the Spring container itself was missed out due to this.

[ 2012-09-05 00:16:43,188 [main] support.DefaultListableBeanFactory.registerBeanDefinition():618  INFO ]: Overriding bean definition for bean 'beanA': replacing [Generic bean: class [com.kilo.spring.context.log.ClassA]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [com/kilo/spring/context/log/spring-application-context-2.xml]] with [Generic bean: class [com.kilo.spring.context.log.ClassA]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [com/kilo/spring/context/log/spring-application-context-1.xml]]
[ 2012-09-05 00:16:43,235 [main] support.DefaultListableBeanFactory.preInstantiateSingletons():555  INFO ]: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8453227: defining beans [org.springframework.beans.factory.config.MethodInvokingFactoryBean#0,beanB,beanA]; root of factory hierarchy

如果通过系统属性 log4j.configuration 配置了名称,则可以查看日志.我想如果将配置作为静态块放在其中一个类中,则可以消除这种情况-但是在Web应用程序中,这样做似乎很奇怪.我可以使用其他技巧吗?请随意指出我在这里遵循的所有/所有不正确的范例.

If I configured the name via the system property log4j.configuration I am able to see the logs. I guess this can go away if we put the configuration as a static block in one of the classes - but in a web application, this seemed strange to do. Any other tricks that I could use? Feel free to point out any/all incorrect paradigms that I am following here.

提前谢谢!

推荐答案

在Tomcat中,您可以在tomcats context.xml

In Tomcat you can configure such a string in the tomcats context.xml

<Parameter name="log4j.configuration" value="whereEver"/>

另一种方法是通过JNDI进行配置.

An other way would be configuration via JNDI.

BTW阅读了此问题使用Spring初始化Log4J吗?,它包含一个链接(在可接受的答案)的实现,该实现通过servlet侦听器中的jndi配置log4j.

BTW read this question Initializing Log4J with Spring?, it contains a link (in the comment of the accepted answer) to an implementation that configures log4j via jndi in a servlet listener.

这篇关于在Spring中正确使用Log4jConfigurer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:43