log4j的辅助代码,支持合并多个log4j配置文件。

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

1。当多人合作开发程序时,都使用了log4j,而且各自写了各自的log4j,properties配置文件 ,

在程序合并的时候,如果要合并配置文件的话,在后期开发维护又会产生一些不便。

//copyright © li zong bo

而log4j自身的配置信息,在读取新配置文件的时候,老配置文件的信息不会叠加。

因此自己写了个下面的方法。用来兼容老的配置信息的读取,也增强了配置文件的灵活性。

使用方法,只要配置文件名字满足log4j*.properties,再指定配置文件存放的文件夹就可以了。

/*

import org.apache.log4j.PropertyConfigurator;

PropertyConfigurator.configure(Properties properties);

使用这个方法来调用即可。

//copyright © li zongbo

*/

public static Properties loadproperties(String dir)

throws FileNotFoundException, IOException {

return loadproperties(dir, null);

}

//copyright © lizongbo

public static Properties loadproperties(String dir, String fileNamestartWith)

throws FileNotFoundException, IOException {

if (fileNamestartWith == null) {

fileNamestartWith = "log4j";

}

//copyright © li zongbo

Properties pro = new Properties();

File fdir = new File(dir);

//copyright © lizong bo

if (fdir.isFile()) {

if (fdir.getName().toLowerCase().endsWith(".properties")) {

pro.load(new FileInputStream(fdir));

}

} else {

File[] fspro = fdir.listFiles();

//copyright © lizongbo

for (int i = 0; i < fspro.length; i++) {

if (fspro[i].getName().startsWith(fileNamestartWith) &&

fspro[i].getName().toLowerCase().endsWith(".properties")) {

Properties temppro = new Properties();

temppro.load(new FileInputStream(fspro[i]));

//copyright © lizongbo

//copyright © lizongbo

for (Enumeration e = temppro.keys(); e.hasMoreElements();) {

String key = (String) e.nextElement();

pro.setProperty(key, temppro.getProperty(key));

}

}

}

}

//copyright © lizongbo

//copyright © lizongbo

return pro;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有