用Spring初始化Log4J? [英] Initializing Log4J with Spring?
问题描述
我有一个使用Spring的 Log4jConfigurer
类的Web应用程序来初始化我的Log4J日志工厂。基本上它使用不在类路径的配置文件初始化Log4J。
I have a web app that uses Spring's Log4jConfigurer
class to initialize my Log4J log factory. Basically it initializes Log4J with a config file that is off the class path.
这是配置:
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="sbeHome">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>#{ MyAppHome + '/conf/log4j.xml'}</value>
</list>
</property>
</bean>
但是我在应用程序启动时收到此错误:
However I get this error at application startup:
log4j:警告没有找到记录器的追加器
并且大量的Spring应用程序上下文初始化消息是打印到控制台。我认为这是因为Spring在有机会初始化我的记录器之前正在开始初始化我的应用程序。如果它很重要,我在Log4J上使用SLF4J。
and tons of Spring application context initialization messages are printed to the console. I think this is because Spring is doing work to initialize my application before it has a chance to initialize my logger. In case it matters, I am using SLF4J on top of Log4J.
有没有什么方法可以让我的Log4jConfigurer成为第一个初始化的bean?或者还有其他方法可以解决这个问题吗?
Is there some way I can get my Log4jConfigurer to be the first bean initialized? or is there some other way to solve this?
推荐答案
您可以在web.xml中配置Log4j监听器而不是弹簧-context.xml
You could configure your Log4j listener in the web.xml instead of the spring-context.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.web.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
所以在春季开始之前就已经开始了。
So it is up before Spring starts.
这篇关于用Spring初始化Log4J?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!