是否可以在Tomcat中动态重新加载log4j.xml /log4j.properties文件?

ktecyv1j  于 9个月前  发布在  其他
关注(0)|答案(8)|浏览(141)

问题是,无论何时更改log4j.properties/log4j.xml,都需要重新启动tomcat [或其他服务器]。

oxosxuxt

oxosxuxt1#

你可以通过以下简单的步骤来编写一个小的初始化器代码:

  • 监听“BEFORE_START_EVENT”,
  • 当事件发生时(每次Tomcat重启一次),使用configureAndWatch方法启动log4j
  • 另外,不要忘记安装一个关闭钩子来清理监视器线程

详情请参阅此博客文章-reload log4j configuration in tomcat
他们也把它移到了github

nle07wnf

nle07wnf2#

更新:如果您使用的是lg4j2.xml,那么在运行时管理log4j只需要配置即可

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
  <Loggers>
-------
  </Loggers>
</Configuration>

字符串
监视器间隔30加载log4j每30秒更改一次。
下面的解决方案是如果你是在旧版本的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>


完整的代码和解释可以在这里找到
Changing log4j Level dynamically

0dxa2lsx

0dxa2lsx3#

你可以创建一个strut action或者一个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");
    */

    }
}

字符串

au9on6nz

au9on6nz4#

另一种方法是在web.xml中配置Spring Framework的Log4jConfigListener

dvtswwa3

dvtswwa35#

Guido Garcia的回答非常准确。
Log4j 1提供了一种以非JEE线程安全方式重新加载log4j配置的方法。
因此,如果你是一个JEE continer,你可以通过以下方式解决你的问题:
(A)创建您的@Singleton ejb计时器,以定期扫描您的log4j.properties文件
(b)看看log4j给出的log4j日志监视的实现。当重新加载文件时,它会做什么,非常简单方便,如下所示:
PropertyConfigurator().doConfigure(filename,LogManager.getLoggerRepository());
如果配置文件上的时间戳发生了变化,也可以这样做。

0yg35tkg

0yg35tkg6#

另一种方法是使用Java File WatcherService配置文件监视器,如下所述链接并在任何文件修改时重新加载Log4J配置。
https://dzone.com/articles/how-watch-file-system-changes
可以使用DOMplayer的API来完成扩展
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html

ergxz8rk

ergxz8rk7#

来自http://logging.apache.org/log4j/1.2/faq.html#3.6
如果配置文件改变了,有没有办法让log4j自动重新加载它?
可以,DOMPlayer和PropertyPlayer都支持通过configureAndWatch方法自动重装,详情请参考API文档。
因为configureAndWatch启动了一个单独的wathdog线程,而且在log4j 1.2中没有办法停止这个线程,所以configureAndWatch方法在应用程序被回收的J2EE环境中使用是不安全的。
也就是说,我已经在Java EE环境(Sun One Web Server,而不是Tomcat)中成功地使用了PropertyMonitorator #configureAndWatch方法。

5m1hhzi4

5m1hhzi48#

从log4j 2.x开始,您可以定期重新加载配置,在本示例中为每30秒重新加载一次:

<configuration monitorInterval="30">

字符串
请在这里查看有关log4j 2.x配置的更多信息。

相关问题