java 使用 web.xml 中的 org.springframework.web.util.Log4jConfigListener 条目进行初始化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10169851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialization using org.springframework.web.util.Log4jConfigListener entry in web.xml?
提问by M Sach
Hi Guys. In most of the Java projects using Spring I find this entry in web.xml which is executed at server start-up:
嗨,大家好。在大多数使用 Spring 的 Java 项目中,我在 web.xml 中找到了这个条目,它在服务器启动时执行:
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
What is the purpose of Log4jConfigListener
?
的目的是Log4jConfigListener
什么?
In my legacy project I can also see this setting. However when i dive into the code I do not find anything special done in this class or further classes called internally by this class. I am sure there must be some good purpose behind putting above code snippet and I am missing it.
在我的旧项目中,我也可以看到此设置。然而,当我深入研究代码时,我没有发现在这个类或这个类内部调用的其他类中做了什么特别的事情。我确信将上面的代码片段放在上面一定有一些好的目的,但我错过了它。
In every class which putting the logs in file here is the entry
在将日志放入文件的每个类中,这里是条目
private static final Log log = LogFactory.getLog(PoolManagerImpl.class);
log.debug("Number of connection pools to create = ["
+ connection.size() + "]");
Even if i comment out my web.xml entry, logging works fine. so what is it's purpose?
即使我注释掉我的 web.xml 条目,日志记录也能正常工作。那么它的目的是什么?
回答by Charles Anthony
The Log4jConfigListener
initialises the Log4j "subsystem" as soon the webapplication starts up, as opposed to "lazily configuring" it as soon as it is needed.
在Log4jConfigListener
初始化Log4j的“子系统”尽快的web应用启动时,为尽快它是需要反对“懒洋洋地配置”它。
In my opinion, the main advantage of explicitly initializing log4j via the Log4JConfigListener is that it allows you to configure the location of the log4j configuration file through using servlet context parameters; depending on how the application is deployed, this may make it possible for the configuration to be changed at runtime by some kind of admin user without having to dig around inside the exploded WAR directory .
在我看来,通过 Log4JConfigListener 显式初始化 log4j 的主要优点是它允许您通过使用 servlet 上下文参数来配置 log4j 配置文件的位置;根据应用程序的部署方式,这可能使某种管理员用户在运行时更改配置成为可能,而无需在展开的 WAR 目录中挖掘。
See the javadoc for Log4jConfigListener, and more importantly Log4jWebConfigurer, as it does the real work.
请参阅Log4jConfigListener的 javadoc ,更重要的是Log4jWebConfigurer,因为它执行实际工作。