Correctly using Log4jConfigurer in Spring

不打扰是莪最后的温柔 提交于 2020-01-03 20:09:09

问题


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>

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

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.

Thanks in advance!


回答1:


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

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

An other way would be configuration via JNDI.

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.



来源:https://stackoverflow.com/questions/12275025/correctly-using-log4jconfigurer-in-spring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!