我试图使用Log4j作为Spring Framework的一部分,据我所知,通过使用适当的bean,系统应该映射代码中可访问的单例实例,同时自动将日志记录深度映射到类
类似于Log4J的正常使用
Logger log = Logger.getLogger(getClass());
我一直在使用以下Spring bean定义
<bean id="log4jInitialization"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>conf\log4j.xml</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
但我无法将此bean映射到给定类中的特定成员,也无法通过@autowired使用它
如果有更好的方法来集成Log4j和Spring,请告诉我
最好的祝福
标记
对你的问题的简短回答是log4j不是DI友好的.
该Log4jConfigurer.initLogging()方法具有void返回值,因此无需注入.我们的想法是你调用那个引导log4j的方法,然后像往常一样使用Log4j API(使用Logger.getLogger(getClass())).
但是,您通常不会将其配置Log4jConfigurer为Spring bean,但通常在应用程序启动期间直接从您自己的代码调用它.
如果这是一个webapp,那么Spring提供了Log4jConfigurer更适合该环境的替代方案(Log4jWebConfigurer,Log4jConfigListener).
顺便提一下,2年前我提交了一个功能请求,允许记录器自动装配,最后它被标记为Spring 3.1的修复程序.Horray.
| 归档时间: |
|
| 查看次数: |
4901 次 |
| 最近记录: |