但这只是行不通.每当我尝试重新读取配置时,都会禁用日志记录.
请参阅以下代码段:
package test; import java.io.FileInputStream; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; public class A { private static final Logger LOGGER= Logger.getLogger(A.class.getName()); public static void main(String[] args) throws Exception { System.out.println("--- start"); LOGGER.log(Level.SEVERE,"SEVERE 1"); LOGGER.log(Level.FINEST,"FINEST 1"); LogManager.getLogManager().readConfiguration(); LOGGER.log(Level.SEVERE,"SEVERE 2"); LOGGER.log(Level.FINEST,"FINEST 2"); LogManager.getLogManager().readConfiguration(new FileInputStream("/tmp/logging.properties")); LOGGER.log(Level.SEVERE,"SEVERE 3"); LOGGER.log(Level.FINEST,"FINEST 3"); System.out.println("--- end"); } }
如果我运行没有任何VM参数的类,这是输出:
--- start 09.11.2012 09:59:25 test.A main SCHWERWIEGEND: SEVERE 1 09.11.2012 09:59:25 test.A main SCHWERWIEGEND: SEVERE 2 --- end
如您所见,仅记录SEVERE级别,因为这是JREs logging.properties的默认值.调用LogManager #readConfiguration()不会改变任何东西.但是当我尝试从logging.properties读取配置时,绝对不会记录任何内容.调用LogManager #readConfiguration(InputStream)或设置java.util.logging.config.file属性并调用LogManager #readConfiguration()没有区别.
现在看下一个输出,当我使用VM属性-Djava.util.logging.config.file = / tmp / logging.properties运行相同的代码时:
--- start 2012-11-09 10:03:44.0838 SEVERE [test.A#main()] - SEVERE 1 2012-11-09 10:03:44.0843 FINEST [test.A#main()] - FINEST 1 --- end
如您所见,SEVERE和FINEST级别都会被记录,并以不同的格式记录.两者都在我的自定义logging.properties中指定.
但是在调用LogManager #readConfiguration()之后,日志记录停在这里!这与上面的例子不同,我不明白.此外,就像上面的示例一样,调用LogManager #readConfiguration(InputStream)不起作用.
那么问题是什么?根据javadoc设置,运行时的java.util.logging.config.file属性应该可以工作.另外两个readConfiguration()方法都应该像我期望的那样工作.那么问题是什么?
解决方法
也许您的根记录器级别低于FINEST,例如信息(.level = INFO).
或者根本没有设置,在这种情况下我认为它是INFO.
我用以下logging.properties运行你的代码:
handlers=java.util.logging.ConsoleHandler .level=FINEST java.util.logging.ConsoleHandler.level=FINEST
没有指定-Djava.util.logging.config.file = / tmp / logging.properties输出是:
--- start 09.11.2012 14:25:49 testing.Scribble main SCHWERWIEGEND: SEVERE 1 09.11.2012 14:25:49 testing.Scribble main SCHWERWIEGEND: SEVERE 2 09.11.2012 14:25:49 testing.Scribble main SCHWERWIEGEND: SEVERE 3 09.11.2012 14:25:49 testing.Scribble main AM FEINSTEN: FINEST 3 --- end
看起来不错!
(我的测试类称为testing.Scribble,这是唯一的区别)
使用-Djava.util.logging.config.file = / tmp / logging.properties输出是:
--- start 09.11.2012 14:31:06 testing.Scribble main SCHWERWIEGEND: SEVERE 1 09.11.2012 14:31:06 testing.Scribble main AM FEINSTEN: FINEST 1 09.11.2012 14:31:06 testing.Scribble main SCHWERWIEGEND: SEVERE 2 09.11.2012 14:31:06 testing.Scribble main AM FEINSTEN: FINEST 2 09.11.2012 14:31:06 testing.Scribble main SCHWERWIEGEND: SEVERE 3 09.11.2012 14:31:06 testing.Scribble main AM FEINSTEN: FINEST 3 --- end
看起来也正确!