一、使用log4j自带的动态更新配置文件的方法
使用log4j自带的动态更新配置很简单,只要调用 PropertyConfigurator 或者 DOMConfigurator类的 configureAndWatch(String configFileName)或者 configureAndWatch(String configFileName, long delay)方法就可以了。
<wbr></wbr>
实现的原理
在调用PropertyConfigurator 或者 DOMConfigurator类的configureAndWatch方法时log4j会创建一个线程,定时的去检查配置文件是否改变,如果改变的话就重新加载配置文件。
configureAndWatch方法参数的介绍:
String configFileName:配置文件的路径加文件名。
long delay:扫描配置文件是否改变的间隔时间。默认值是 60 秒。
注意:在log4j中每调用一次configureAndWatch方法都会启动一个新的扫描线程。
实例代码:
package com.kun;
使用log4j自带的动态更新配置很简单,只要调用 PropertyConfigurator 或者 DOMConfigurator类的 configureAndWatch(String configFileName)或者 configureAndWatch(String configFileName, long delay)方法就可以了。
<wbr></wbr>
实现的原理
在调用PropertyConfigurator 或者 DOMConfigurator类的configureAndWatch方法时log4j会创建一个线程,定时的去检查配置文件是否改变,如果改变的话就重新加载配置文件。
configureAndWatch方法参数的介绍:
String configFileName:配置文件的路径加文件名。
long delay:扫描配置文件是否改变的间隔时间。默认值是 60 秒。
注意:在log4j中每调用一次configureAndWatch方法都会启动一个新的扫描线程。
实例代码:
package com.kun;
<wbr><br><span style="color:#993399">import org.apache.log4j.Logger;</span><br style="color:rgb(153,51,153)"><br style="color:rgb(153,51,153)"><span style="color:#993399">import org.apache.log4j.PropertyConfigurator;</span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399">public class TestLog4j</span><br style="color:rgb(153,51,153)"><span style="color:#993399">{</span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> public static Logger logger = Logger.getLogger(TestLog4j.class);</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> static</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> {</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> PropertyConfigurator.configureAndWatch("./log4j.properties", 60000);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> }</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> public static void printLog()</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> {</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> if (logger.isDebugEnabled())</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> logger.debug("debug!!");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> if (logger.isInfoEnabled())</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> logger.info("info!!");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> logger.error("error!!");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> }</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> public static void main(String[] args)</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> {</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> while (!Thread.interrupted())</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> TestLog4j.printLog();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> try</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> Thread.sleep(1000);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> } catch (InterruptedException e)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399"><wbr><wbr><wbr> }</wbr></wbr></wbr></span><br style="color:rgb(153,51,153)"><span style="color:#993399">}</span><br><br><br><span style="font-weight:bold">二、自己创建动态更新配置文件的类和方法</span><br><br><wbr><wbr><wbr><wbr> 在第一章中我们可以通过PropertyConfigurator的 configureAndWatch(String configFileName, long delay)方法来进行动态的改变log4j的配置,但是他采用了轮询方式来实现的,现在我们需要触发机制来完成这个功能。<br><br><span style="font-weight:bold">实现的原理</span><br><br><wbr><wbr><wbr> 开始的思考中,认为应该通过自定义一套log4j的配置信息,然后启动log4j的同时启动一个server ,socket或者web service一类的服务,然后再通过消息来进行log4j的配置。后来发现当调用PropertyConfigurator对象的configure(String configFilename)方法时就可以重新配置log4j。<br><br><wbr><wbr><wbr> 但是我们还不能确定log4j系统正在运行时,再次调用configure(String configFilename)方法,会不会对系统运行的产生其他的影响?当查看log4j源码后这个问题被解决了。因为 PropertyConfigurator的 configureAndWatch(String configFileName, long delay)方法就是启动一个线程并在这个线程中反复的实例化一个PropertyConfigurator对象,并调用他的 doConfigure(String
configFileName, LoggerRepository hierarchy)方法,而PropertyConfigurator对象的configure(String configFilename)也是实例化一个PropertyConfigurator对象,并调用他的doConfigure(String configFileName, LoggerRepository hierarchy)方法。所以可以确定在log4j运行时调用configure(String configFilename)方法是不会对系统造成什么不良影响的,除非log4j本身就存在问题。<br><br><wbr><wbr><wbr><wbr> 现在问题就比较简单了,就是如何调用configure(String configFilename)方法了。这个当然就和实际的业务分不开了,如果是一个web应用,那当然是通过页面来调用。如果是web service应用,那当然是使用开发的服务来调用。这个就要根据实际的应用来确定了。<br><br><span style="font-weight:bold">实例代码:</span><br>
这是一个WEB方式的调用代码:<br>
TestLog4jReload.java:<br><br><span style="color:#9933cc">package com.kun;</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">import org.apache.log4j.PropertyConfigurator;</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">public class TestLog4jReload</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">{</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr><wbr> public static void reloadLog4jConfig()</wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr> {</wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> PropertyConfigurator.configure("./log4j.properties");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr> }<wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">}</span><br><br>
TestLog4jReload.jsp:<br><br><span style="color:#9933cc"><%@ page contentType = "text/html;charset=UTF-8" language = "java" %></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><%@ page import = "java.util.*, java.io.*" %></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><%@ page import = "com.kun.TestLog4jReload %></span><br style="color:rgb(153,51,204)"><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><%</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr> String state = request.getParameter("state");</wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr> if(state != null && state.trim().equals("start"))</wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr> {</wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr><wbr><wbr><wbr><wbr> TestLog4jReload.reloadLog4jConfig();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><wbr><wbr> }</wbr></wbr></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">%></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><HTML></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><HEAD></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr> <TITLE> 动态更新log4j配置 </TITLE></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr></HEAD></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr><BODY></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr> <A HREF="TestLog4jReload.jsp?state=start">开始</A></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr></BODY></wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"></HTML></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>