[英]Configure java.util.logging with apache commons-configuration PropertiesConfiguration?
我想加載屬性文件和命令行參數,然后在運行時動態配置日志記錄,我以前可以這樣做:
Properties configuration;
...
ByteArrayOutputStream os = new ByteArrayOutputStream();
ByteArrayInputStream is;
byte[] buf;
try {
configuration.store(os, "logging");
buf = os.toByteArray();
is = new ByteArrayInputStream(buf);
java.util.logging.LogManager.getLogManager().readConfiguration(is);
} catch (IOException e) {
System.err.println("Failed to configure java.util.logging.LogManager");
}
偉大的屬性,但可以用PropertiesConfiguration嗎?
(僅供參考,我希望利用公共配置提供的屬性數組)
使用ConfigurationConverter將PropertiesConfiguration轉換為標准Poperties文件
不。 但是您可以將PropertiesConfiguration轉換為Properties
public static Properties configurationAsProperties(){
Properties fromConfiguration = new Properties();
Iterator<String> keys = configuration.getKeys();
while (keys.hasNext()) {
String key = keys.next();
String value = asString(configuration.getProperty(key));
fromConfiguration.setProperty(key,value);
// System.out.println(key + " = " + value);
}
return fromConfiguration;
}
只是不要丟失那些逗號分隔的值(configuration.getString將僅返回第一個)
private static String asString(Object value) {
if (value instanceof List) {
List<?> list = (List<?>) value;
value = StringUtils.join(list.iterator(), ",");
}
return (String) value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.