java应用(非web应用)中log4j.properties动态修改配置文件,无需重启,就能立即生效---自动加载

穆思阁 发布于 2012/12/22 15:47
阅读 2K+
收藏 0
java应用(非web应用)中log4j.properties动态修改配置文件,无需重启,就能立即生效---自动加载  主要使用:PropertyConfigurator.configureAndWatch(path,1000);// 间隔特定时间,检测文件是否修改,自动重新读取配置 来实现log4j配置文件修改后自动重载。 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;  } /*  * 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🇲🇲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🇲🇲ss}-%p][%F:%L] %m%n 测试: 运行测试类,然后修改log4j.properties中log4j.rootLogger=info,Console 将info改为error或debug,然后查看输出效果。
加载中
0
Ryan-瑞恩
Ryan-瑞恩
你修改,把代码整理一下……这样让人看的难受。
0
ddatsh
ddatsh
jrebel无压力
返回顶部
顶部