如何使用属性文件设置java日志logging? (java.util.logging的)

我有一个愚蠢的Java日志logging问题:我从我的应用程序configuration文件加载日志loggingconfiguration – 但它只是没有logging后读取文件(这看起来很像你会发现在networking上的例子,除了额外的应用程序configuration – 删除这也没有帮助)。 “初始化…”日志行看起来很好,但“开始的应用程序”和任何进一步的消息都不logging到控制台,也没有创build日志文件。 我在这里错过了什么?

logging器代码如下所示:

... Logger log = Logger.getLogger("myApp"); log.setLevel(Level.ALL); log.info("initializing - trying to load configuration file ..."); Properties preferences = new Properties(); try { FileInputStream configFile = new FileInputStream("/path/to/app.properties"); preferences.load(configFile); LogManager.getLogManager().readConfiguration(configFile); } catch (IOException ex) { System.out.println("WARNING: Could not open configuration file"); System.out.println("WARNING: Logging not configured (console output only)"); } log.info("starting myApp"); ... 

这是configuration文件:

 appconfig1 = foo appconfig2 = bar # Logging handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler .level = ALL # File Logging java.util.logging.FileHandler.pattern = %h/myApp.log java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.FileHandler.level = INFO # Console Logging java.util.logging.ConsoleHandler.level = ALL 

好的,第一个直觉就在这里:

 handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler .level = ALL 

Java prop文件parsing器并不是那么聪明,我不知道它会处理这个。 但我会再看看文档….

同时,请尝试:

 handlers = java.util.logging.FileHandler java.util.logging.ConsoleHandler.level = ALL 

更新

不,这需要更多的咖啡。 没关系。

当我想到更多的时候,请注意,您可以使用Properties中的方法来加载和打印一个prop文件:可能需要编写一个最简单的程序来查看java认为它在该文件中读取的内容。


另一个更新

这一行:

  FileInputStream configFile = new FileInputStream("/path/to/app.properties")); 

有一个额外的结束。 它不会编译。 确保你正在使用你认为是的类文件。

您可以通过命令行设置您的日志configuration文件:

$ java -Djava.util.logging.config.file=/path/to/app.properties MainClass

这种方式似乎更清洁,更容易维护。

我已经在上面的代码中尝试过你的代码,不要使用[preferences.load(configFile);]语句,它会工作。有运行示例代码

 public static void main(String[]s) { Logger log = Logger.getLogger("MyClass"); try { FileInputStream fis = new FileInputStream("p.properties"); LogManager.getLogManager().readConfiguration(fis); log.setLevel(Level.FINE); log.addHandler(new java.util.logging.ConsoleHandler()); log.setUseParentHandlers(false); log.info("starting myApp"); fis.close(); } catch(IOException e) { e.printStackTrace(); } } 

您是否正在以正确的pathsearch日志文件:%h / one%u.log

这里%hparsing到你的家:在Windows中,这默认为:C:\ Documents and Settings(user_name)。

我已经尝试过你已经发布的示例代码,并且在指定configuration文件path(通过代码或java参数指定logging.properties)后,它工作正常。

 Logger log = Logger.getLogger("myApp"); log.setLevel(Level.ALL); log.info("initializing - trying to load configuration file ..."); //Properties preferences = new Properties(); try { //FileInputStream configFile = new //FileInputStream("/path/to/app.properties"); //preferences.load(configFile); InputStream configFile = myApp.class.getResourceAsStream("app.properties"); LogManager.getLogManager().readConfiguration(configFile); } catch (IOException ex) { System.out.println("WARNING: Could not open configuration file"); System.out.println("WARNING: Logging not configured (console output only)"); } log.info("starting myApp"); 

这是工作.. :)你必须在readConfiguration()中传递InputStream。