用 Spring 初始化 Log4J? [英] Initializing Log4J with Spring?
问题描述
我有一个 Web 应用程序,它使用 Spring 的 Log4jConfigurer
类来初始化我的 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:WARN 找不到记录器的附加程序
和大量 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 而不是 spring-context.xml 中配置你的 Log4j 监听器
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屋!