为了在小的Java桌面应用程序使用记录我想深入了解的一些方法操作。 我用一个非常愚蠢和Java小程序来测试它们。
特别是,测试LogManager.readConfiguration()方法的行为时,我发现了一些奇怪的。 在所有测试的LogManager读取位于JRE的lib目录/ logging.properties的属性文件及其配置。 在这个时候,这个文件的内容如下:
handlers=java.util.logging.ConsoleHandler
myapp2.handlers=java.util.logging.ConsoleHandler
myapp2.MyApp2.handlers=java.util.logging.ConsoleHandler
.level=OFF
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
myapp2.level=WARNING
myapp2.MyApp2.level=INFO
Java程序的代码是:
package myapp2;
import java.io.IOException;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class MyApp2 {
private static final Logger LOGGER = Logger.getLogger(MyApp2.class.getPackage().getName());
private static final Logger LOGGER1 = Logger.getLogger(MyApp2.class.getName());
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
LOGGER.severe("severe at MyApp2");
LOGGER.warning("warning at MyApp2");
LOGGER.info("info at MyApp2");
LOGGER1.severe("severe1 at MyApp2");
LOGGER1.warning("warning1 at MyApp2");
LOGGER1.info("info1 at MyApp2");
LOGGER1.setLevel(null);
LOGGER1.setUseParentHandlers(false);
LOGGER1.severe("severe2 at MyApp2");
LOGGER1.warning("warning2 at MyApp2");
LOGGER1.info("info2 at MyApp2");
try {
LogManager.getLogManager().readConfiguration();
} catch (IOException ex) {
System.out.println("I/O Exception found");
} catch (SecurityException ex) {
System.out.println("Error SecurityException found");
}
LOGGER.severe("severe3 at MyApp2");
LOGGER1.severe("severe4 at MyApp2");
}
}
如果我们执行它而不围绕readConfiguration()在try-catch,按预期工作,输出如下:
SEVERE: severe at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe2 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning2 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe3 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe3 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe4 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
但是,如果我们在try-catch执行时,输出为:
SEVERE: severe at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe2 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning2 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe3 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
读取readConfiguration()方法的API它应该重新初始化日志属性并重新读取从先前指定的文件中的记录的配置。 如果是这样,为什么severe3只显示一次(我希望显示,因为这两个记录器的两倍存在于程序和转发行为),并severe4丢失(我期望能够显示一次)? 可能有人帮我这个已了解?