使用 Spring 初始化 Log4J?
我有一个 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 No Appender can be find for logger
并且大量 Spring 应用程序上下文初始化消息被打印到控制台。我认为这是因为 Spring 在有机会初始化我的记录器之前正在初始化我的应用程序。以防万一,我在 Log4J 之上使用 SLF4J。
有什么方法可以让我的 Log4jConfigurer 成为第一个初始化的 bean 吗?或者还有其他方法可以解决这个问题吗?
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.
Here is the config:
<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 No appenders could be found for logger
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在 web.xml 而不是 spring-context.xml 中配置 Log4j 侦听器,
因此它会在 Spring 启动之前启动。
You could configure your Log4j listener in the web.xml instead of the spring-context.xml
So it is up before Spring starts.
我们的独立应用程序需要一个
SMTPAppender
,其中电子邮件配置已存在于spring
配置文件中,并且我们不希望在log4j.properties< 中重复该配置/代码>。
我将以下内容放在一起,使用 spring 添加一个额外的附加程序。
我们的类路径上还有一个
log4j.properties
文件,其中详细介绍了我们常规的FileAppenders
。我意识到这对于您的需求来说可能有点过分了:)
Our standalone application required an
SMTPAppender
where the email config already exists in aspring
config file and we didn't want that to be duplicated in thelog4j.properties
.I put the following together to add an extra appender using spring.
We also have a
log4j.properties
file on the classpath which details our regularFileAppenders
.I realise this may be overkill for what you require :)
与其在代码中自己配置 log4j,为什么不通过添加
到服务器的启动属性来将 log4j 指向(自定义)配置文件的位置呢?
更好的是,只需将 log4j.xml 移动到默认位置(在类路径上),然后让 log4j 自动配置自身。
Rather than configuring log4j yourself in code, why not just point log4j at your (custom) configuration file's location by adding
to your server's startup properties?
Even better, just move log4j.xml to the default location - on the classpath - and let log4j configure itself automatically.
您可以使用类路径而不是硬编码路径。这对我有用
You can use classpath instead of hardcoded path. It works for me
如果您使用的是 Jetty,则可以在每个应用程序的基础上添加额外的类路径:
http://wiki .eclipse.org/Jetty/Reference/Jetty_Classloading#Adding_Extra_Classpaths_to_Jetty
这将允许您以标准方式(从类路径:)
在 web.xml 中加载 log4 属性:
在 jetty-web.xml 中:
If you are using Jetty you can add extra classpaths on a per application basis:
http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading#Adding_Extra_Classpaths_to_Jetty
This will allow you to load your log4 properties in a standard manner (from the classpath:)
in web.xml:
in jetty-web.xml: