[英]How to configure log4j.properties in external folder for a web application in tomcat
Please help how to handle the below scenario. 请帮助如何处理以下情况。
Current Web app : I have placed log4j.propertes in /WEB-INF/classes
folder of webapp. 当前的Web应用程序: 我已将log4j.propertes放置在webapp的/WEB-INF/classes
文件夹中。
Wanted to achieve : Place log4j.properties in an external location and refer from my web application, so that i can modify the log location when ever i want without re building the war. 想要实现: 将log4j.properties放置在外部位置并从我的Web应用程序引用,这样我就可以在需要时随时修改日志位置,而无需重建战争。
$CATALINA_HOME/propdirwebapp1/log4j.properties $CATALINA_HOME/propdirwebapp2/log4j.properties
If i have multiple applications deployed please suggest the solution. 如果我部署了多个应用程序,请提出解决方案。 If only one application is deployed on server please suggest the solution 如果服务器上仅部署了一个应用程序,请提出解决方案
In my case in web.xml
I added Log4jConfigListener
and it worked. 就我在web.xml
情况而言,我添加了Log4jConfigListener
并成功了。 log4jRefreshInterval
is optional here. log4jRefreshInterval
在此处是可选的。
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>file:/<your-path-here>/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<!-- Refresh log4j configuration every 5 minutes. -->
<param-value>300000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
无需重建war文件,只需直接修改log4j.properties并重新启动tomcat。
You could add a directory to shared.loader property in the $CATALINA_BASE/conf/catalina.properties. 您可以在$ CATALINA_BASE / conf / catalina.properties中的shared.loader属性中添加目录。
shared.loader=conf/myapp/
Then put the log4j.properties in this directory. 然后将log4j.properties放在此目录中。
But there are two constraints. 但是有两个约束。
1) If there is a log4j.properties file in the WEBINF/classes
that one will get loaded first 1)如果WEBINF/classes
中有一个log4j.properties文件, WEBINF/classes
首先加载该文件
2) This directory gets shared by all webapps so there can only by one log4j.properties file for all webapps. 2)该目录被所有Web应用共享,因此所有Web应用只能有一个log4j.properties文件。
I like my attempt, it just deals with log levels for different war applications. 我喜欢我的尝试,它只处理不同战争应用程序的日志级别。
Place a custom properties file in conf, for example 例如,将自定义属性文件放入conf中
$CATALINA_BASE/conf/my.properties
Create entries like 创建条目,例如
myapp.loglevel = INFO
myapp2.loglevel = DEBUG
Read the props per request, for example 阅读每个请求的道具,例如
myProps = new Properties();
myProps.load(new FileInputStream(new File(System.getProperty("catalina.base"), "conf/my.properties")));
Read and use the property like for example in a servlet at the beginning of a request 读取并使用该属性,例如在请求开始时在servlet中
String myLoglevel = (String)myProps.get("myapp.loglevel");
LogManager.getRootLogger().setLevel(Level.toLevel(myLoglevel));
We're using slf4j-log4j12 and I've read there's no LogManager in log4j2, but I'm sure this mechanism could be applied to log4j2, too. 我们正在使用slf4j-log4j12,并且我读到log4j2中没有LogManager,但是我敢肯定,该机制也可以应用于log4j2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.