在我们的应用程序中,我们决定将log4j配置文件命名为自定义名称,以避免无意中从另一个jar加载默认文件。要配置它,我们使用org.springframework.util.Log4jConfigurer
来指定log4j位置。
<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容器本身的一些有价值的日志记录因此被遗漏了。
[ 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应用程序中,这似乎很奇怪。
我可以使用的任何其他技巧?请随意指出我在这里关注的任何/所有不正确的范例。
提前致谢!
答案 0 :(得分:2)
在Tomcat中,您可以在tomcats context.xml
<Parameter name="log4j.configuration" value="whereEver"/>
另一种方式是通过JNDI进行配置。
BTW读了这个问题Initializing Log4J with Spring?,它包含一个链接(在接受的答案的注释中)到一个实现,它通过jlet在servlet监听器中配置log4j。