我的网络应用程序使用 apache commons + log4j。
通常 log4j 需要类路径中的配置文件;但我需要将日志记录配置委托给外部文件(我需要在环境中部署 .war,但日志配置(最大大小、位置等)取决于第二个团队。
我的类路径中有一个 commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
# log4j.configuration=/absolute/path/where/external/logs/are/log4j.properties
不幸的是,注释行不起作用。
有没有办法使用外部配置文件设置 log4j?
您可以将其设置为系统属性log4j.configuration
属性..例如在 J2SE 应用程序中
java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
请注意,该属性值必须是 URL。
有关更多信息,请阅读“默认初始化过程”部分Log4j 手册。 http://logging.apache.org/log4j/1.2/manual.html
也可以让 ServletContextListener 设置系统属性:
import java.util.Enumeration;
import javax.servlet.*;
public class SystemPropertiesHelper implements
javax.servlet.ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
Enumeration<String> params = context.getInitParameterNames();
while (params.hasMoreElements()) {
String param = (String) params.nextElement();
String value =
context.getInitParameter(param);
if (param.startsWith("customPrefix.")) {
System.setProperty(param, value);
}
}
}
public void contextDestroyed(ServletContextEvent event) {
}
}
然后将其放入您的 web.xml 中(context.xml 也应该可以)
<context-param>
<param-name>customPrefix.property</param-name>
<param-value>value</param-value>
<param-type>java.lang.String</param-type>
</context-param>
<listener>
<listener-class>servletUtils.SystemPropertiesHelper</listener-class>
</listener>
我从这个监听器代码中得到了这个answer https://stackoverflow.com/questions/372686/how-can-i-specify-system-properties-in-tomcat-configuration-on-startup .
我希望这可以帮助你!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)