java應用(非web應用)中log4j.properties動態修改設定檔,無需重啟,就能立即生效,如何?。
主要使用:PropertyConfigurator.configureAndWatch(path,1000);// 間隔特定時間,檢測檔案是否修改,自動重新讀取配置
來實現log4j設定檔修改後自動重載。
Log4jConfig.java
package log4jautoload; import org.apache.log4j.PropertyConfigurator; public class Log4jConfig { private static boolean isReload = true; /** * 裝載log4j設定檔 * @author zhaigx * @DATE 2011-5-28 */ public static void load() { String path = Log4jConfig.class.getClass().getResource("/") .getPath() + "log4j.properties"; //String path="config/log4j.properties"; System.out.println("log4j configfile path=" + path); PropertyConfigurator.configureAndWatch(path,1000);// 間隔特定時間,檢測檔案是否修改,自動重新讀取配置 } private static void reload() { if (isReload) { load(); } isReload = false; } public void setReload(boolean flag) { isReload = flag; } }
Logger.java
/* * Created on 2005-6-22 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package log4jautoload; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author Administrator * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class Logger { private Log log = null; static{ Log4jConfig.load();//裝載log4j設定檔 } private Logger() { log = LogFactory.getLog(this.getClass()); } private Logger(Class c) { log = LogFactory.getLog(c); } private Logger(String className) { log = LogFactory.getLog(className); } public static Logger getLogger() { return new Logger(); } public static Logger getLogger(Class c) { return new Logger(c); } public static Logger getLogger(String className) { return new Logger(className); } public void trace(String info) { if (log.isTraceEnabled()) log.trace(info); } public void debug(String info) { if (log.isDebugEnabled()) log.debug(info); } public void info(String info) { if (log.isInfoEnabled()) log.info(info); } public void warn(String info) { if (log.isWarnEnabled()) log.warn(info); } public void error(String info) { if (log.isErrorEnabled()) log.error(info); } public void fatal(String info) { if (log.isFatalEnabled()) log.fatal(info); } public boolean isTraceEnabled() { return log.isTraceEnabled(); } public boolean isDebugEnabled() { return log.isDebugEnabled(); } public boolean isInfoEnabled() { return log.isInfoEnabled(); } public boolean isWarnEnabled() { return log.isWarnEnabled(); } public boolean isErrorEnabled() { return log.isErrorEnabled(); } public boolean isFatalEnabled() { return log.isFatalEnabled(); } }
測試類別:
package log4jautoload; public class BeTestClass { static Logger log=Logger.getLogger(BeTestClass.class); public static void main(String[] args) { for (int i = 0; i < 1000; i++) { log.info("----------info"); log.debug("----------debug"); log.error("----------error"); System.out.println("***********************"); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
log_home=../log log4j.rootLogger=info,Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern= [%d{yyyyMMdd-HH:mm:ss}-%p][%F:%L] %m%n log4j.appender.DailyFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.DailyFile.Threshold = info log4j.appender.DailyFile.File=${log_home}/test.log log4j.appender.DailyFile.encoding=gbk log4j.appender.DailyFile.Append=true log4j.appender.DailyFile.layout=org.apache.log4j.PatternLayout log4j.appender.DailyFile.layout.ConversionPattern=[%d{yyyyMMdd-HH:mm:ss}-%p][%F:%L] %m%n
測試:
運行測試類別,然後修改log4j.properties中log4j.rootLogger=info,Console
將info改為error或debug,然後查看輸出效果。