[英]Is it possible to reload log4j.xml / log4j.properties file dynamically in Tomcat?
问题是,每当你更改log4j.properties/log4j.xml时,你需要重启tomcat [或者说任何其他服务器]。 有没有重新加载log4j配置的解决方法?
来自http://logging.apache.org/log4j/1.2/faq.html#3.6
有没有办法让log4j在配置文件发生变化时自动重新加载?
是。 DOMConfigurator和PropertyConfigurator都支持通过configureAndWatch方法自动重新加载。 有关更多详细信息,请参阅API文档 。
因为configureAndWatch启动了一个单独的wathdog线程,并且由于无法在log4j 1.2中停止此线程,所以configureAndWatch方法在应用程序被回收的J2EE环境中使用是不安全的。
说,我已经在Java EE环境(Sun One Web Server,而不是Tomcat)中成功使用了PropertyConfigurator#configureAndWatch方法。
更新:如果您使用的是lg4j2.xml,那么在运行时管理log4j是唯一需要的配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
<Loggers>
-------
</Loggers>
</Configuration>
监视间隔30每30秒加载log4j更改。
以下解决方案是您使用旧版本的log4j。
是的,您可以在运行时更改log4j级别,而无需重新启动服务器,前提是您使用的是spring。
public class OptionalLog4jConfigurer extends Log4jConfigurer implements
InitializingBean {
public static final Long DEFAULT_REFRESH = 30000L;
private static final Log LOG = LogFactory
.getLog(OptionalLog4jConfigurer.class);
private String configLocation;
private Long refreshInterval;
public OptionalLog4jConfigurer(final String configLocation,
final Long refreshInterval) {
this.configLocation = configLocation;
if (refreshInterval == null) {
this.refreshInterval = DEFAULT_REFRESH;
}
else {
this.refreshInterval = refreshInterval;
}
}
public void afterPropertiesSet() throws Exception {
if (!StringUtils.isEmpty(this.configLocation)) {
LOG.info("Log4J configuration is being customized.");
this.initLoggingInternal();
}
else {
LOG
.info("Using default Log4J configuration. No customization requested");
}
}
public String getConfigLocation() {
return this.configLocation;
}
public Long getRefreshInterval() {
return this.refreshInterval;
}
}
然后对applicationContext进行这些更改。
<bean id="optionalLog4jInitialization" class="com.skg.jetm.OptionalLog4jConfigurer">
<constructor-arg index="0" type="java.lang.String" value="${log4j.configuration}" />
<constructor-arg index="1" type="java.lang.Long" value="100" />
</bean>
完整的代码和解释可以在这里找到
您可以使用以下简短步骤编写一些初始化代码:
有关详细信息,请参阅此博客文章 - 在tomcat中重新加载log4j配置
他们还把它搬到了github 。
您可以创建一个strut操作或一个重新加载属性文件的servlet。 因此,在编辑log4j.properties文件之后,您将需要调用servlet来重新加载它。
例如:
public class Log4JServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected static Logger log = Logger.getLogger(Log4JTestServlet.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Reload Log4J prop file");
String path = "C:\\GlassFishESBv22\\glassfish\\domains\\domain1\\config\\log4j.properties";
PropertyConfigurator.configure(path);
/*
log.debug("debug message");
log.info("info message");
log.warn("warn message");
log.error("error message");
log.fatal("fatal message");
*/
}
}
另一种方法是在web.xml
配置Spring Framework的Log4jConfigListener
Guido Garcia的回答非常明确。
Log4j 1提供了一种在非JEE线程安全主体中重新加载log4j配置的方法。
因此,如果您在JEE continer中,您可以通过以下方式轻松解决您的问题:
(A)创建@Singleton ejb计时器以定期扫描log4j.properties文件
(b)查看log4j给出的log4j日志监视的实现。 当重新加载文件时,它的作用非常简单方便,如下:
new PropertyConfigurator().doConfigure(filename,LogManager.getLoggerRepository());
如果配置文件上的时间戳发生变化,请执行相同的操作。 这就对了。
另一种方法是使用Java File WatcherService配置文件观察程序,如下所述链接并重新加载任何文件修改上的Log4J配置。
https://dzone.com/articles/how-watch-file-system-changes
可以使用DOMConfigurator的API完成重新加载
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.