Log4J 在系统运行时更改log4j的配置

本文介绍了如何在log4j运行时动态更新配置文件的方法,包括使用log4j自带的动态更新功能和自定义实现动态更新配置文件的类和方法。通过实际代码展示了在web应用和页面中调用配置更新的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用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;
<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">&lt;%@ page contentType = "text/html;charset=UTF-8" language = "java" %&gt;</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">&lt;%@ page import = "java.util.*, java.io.*" %&gt;</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">&lt;%@ page import = "com.kun.TestLog4jReload %&gt;</span><br style="color:rgb(153,51,204)"><br style="color:rgb(153,51,204)"><span style="color:#9933cc">&lt;%</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 &amp;&amp; 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">%&gt;</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">&lt;HTML&gt;</span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr>&lt;HEAD&gt;</wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr> &lt;TITLE&gt; 动态更新log4j配置 &lt;/TITLE&gt;</wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr>&lt;/HEAD&gt;</wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr>&lt;BODY&gt;</wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr> &lt;A HREF="TestLog4jReload.jsp?state=start"&gt;开始&lt;/A&gt;</wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc"><wbr>&lt;/BODY&gt;</wbr></span><br style="color:rgb(153,51,204)"><span style="color:#9933cc">&lt;/HTML&gt;</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值