java應用(非web應用)中log4j.properties動態修改設定檔,無需重啟,就能立即生效,如何??__web

來源:互聯網
上載者:User

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,然後查看輸出效果。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.