Is it possible to reload log4j.xml / log4j.propert

2019-02-02 02:56发布

The problem is, whenever you change the log4j.properties/log4j.xml, you need to restart the tomcat [ or say any other server ]. Is there any workaround of reloading the log4j configuration?

8条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-02 03:20

You can write a little initializer code with the following short steps:

  • listen for the "BEFORE_START_EVENT",
  • when the event happens (once per Tomcat restart), start log4j using the configureAndWatch method
  • also don't forget to install a shutdown hook to cleanup the watcher thread

See this blog post for details - reload log4j configuration in tomcat

They also moved it to github.

查看更多
闹够了就滚
3楼-- · 2019-02-02 03:27

Another Method is to configure a file watcher using Java File WatcherService as explained below link and reload Log4J configuration on any file Modifications.

https://dzone.com/articles/how-watch-file-system-changes

Reloading can be done using DOMConfigurator's APIs

https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html

查看更多
成全新的幸福
4楼-- · 2019-02-02 03:31

Another way is to configure Spring Framework's Log4jConfigListener in web.xml

查看更多
神经病院院长
5楼-- · 2019-02-02 03:36

From http://logging.apache.org/log4j/1.2/faq.html#3.6

Is there a way to get log4j to automatically reload a configuration file if it changes?

Yes. Both the DOMConfigurator and the PropertyConfigurator support automatic reloading through the configureAndWatch method. See the API documentation for more details.

Because the configureAndWatch launches a separate wathdog thread, and because there is no way to stop this thread in log4j 1.2, the configureAndWatch method is unsafe for use in J2EE envrironments where applications are recycled.

Said that, I've successfully used PropertyConfigurator#configureAndWatch method in a Java EE environment (Sun One Web Server, not Tomcat).

查看更多
我想做一个坏孩纸
6楼-- · 2019-02-02 03:41

As of log4j 2.x you can reload the config periodically, in this example every 30 seconds:

<configuration monitorInterval="30">

Please take a look here for more information on log4j 2.x configuration:

查看更多
孤傲高冷的网名
7楼-- · 2019-02-02 03:43

You can create a strut action or a servlet which reload the properties file. So after editing the log4j.properties file, you will need to call the servlet to reload it.

For example:

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");
    */

    }
}
查看更多
登录 后发表回答