使用apache commons配置属性配置java.util.logging?
我想加载属性文件和命令行参数,然后在运行时动态配置日志记录,我以前可以这样做:使用apache commons配置属性配置java.util.logging?,java,apache-commons,java.util.logging,Java,Apache Commons,Java.util.logging,我想加载属性文件和命令行参数,然后在运行时动态配置日志记录,我以前可以这样做: Properties configuration; ... ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayInputStream is; byte[] buf; try { configuration.store(os, "logging"); buf = os.toByteArray(); is = new
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");
}
属性很好,但可以通过属性配置完成吗
(仅供参考,我希望利用commons configuration提供的属性阵列)不。但是您可以将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;
}
公共静态属性配置属性(){
Properties fromConfiguration=新属性();
迭代器键=configuration.getKeys();
while(keys.hasNext()){
String key=keys.next();
字符串值=asString(configuration.getProperty(key));
setProperty(键、值);
//System.out.println(键+“=”+值);
}
从配置返回;
}
不要丢失那些逗号分隔的值(configuration.getString将只返回第一个值)
私有静态字符串关联(对象值){
if(列表的值实例){
列表=(列表)值;
value=StringUtils.join(list.iterator(),“,”);
}
返回(字符串)值;
}
用于将属性配置转换为标准权限文件
private static String asString(Object value) {
if (value instanceof List) {
List<?> list = (List<?>) value;
value = StringUtils.join(list.iterator(), ",");
}
return (String) value;
}