(1)讀出log4j文件到Properties對象
?? ?
public Properties getLog4jProperties(){
?? ??? ?Properties p = new Properties();
?? ??? ?InputStream is = this.getClass().getClassLoader().getResourceAsStream("/log4j.properties");
?? ??? ?if(is==null){
?? ??? ??? ?log.warn("read log4j properties file error ");
?? ??? ??? ?return p;
?? ??? ?}
?? ??? ?try {
?? ??? ??? ?p.load(is);
?? ??? ?} catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return p;
?? ?}
?? ?
(2)重新設置屬性
?? ??? ?Properties log4jCfg = getLog4jProperties();
?? ??? ?
?? ??? ?log.info("log4j config properties : "+log4jCfg);
?? ??? ?
?? ??? ?log4jCfg.setProperty("log4j.appender.TASKlog.File", logFile);
?? ??? ?
?? ??? ?log.info("log4j properties prpoperties is : " + log4jCfg);
(3)配置生效?? ??? ?
?? ??? ?
?? ??? ?PropertyConfigurator.configure(log4jCfg);
上面的屬性設置里面,我把原來的日志文件換成了一個新的日志文件。
通過這種方法,就可以實現在任何時候切換到一個新的日志文件開始記錄。
?
|----------------------------------------------------------------------------------------|
版權聲明 版權所有 @zhyiwww
引用請注明來源 http://www.aygfsteel.com/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2009-06-10 16:52
zhyiwww 閱讀(1130)
評論(0) 編輯 收藏 所屬分類:
j2ee