我有一个 Web 应用程序,它使用 Spring 的 Log4jConfigurer
类来初始化我的 Log4J 日志工厂。基本上,它使用不在类路径中的配置文件初始化 Log4J。
这里是配置:
<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>
但是我在应用程序启动时收到此错误:
log4j:WARN 找不到记录器的附加程序
大量 Spring 应用程序上下文初始化消息被打印到控制台。我认为这是因为 Spring 在有机会初始化我的记录器之前正在初始化我的应用程序。万一这很重要,我在 Log4J 之上使用 SLF4J。
有什么方法可以让我的 Log4jConfigurer 成为第一个初始化的 bean?还是有其他方法可以解决这个问题?
最佳答案
您可以在 web.xml 而不是 spring-context.xml 中配置 Log4j 监听器
<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>
所以它在 Spring 开始之前就起来了。
关于java - 用 Spring 初始化 Log4J?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4400583/