如何使用属性文件设置java日志记录? (java.util.logging的) [英] How to set up java logging using a properties file? (java.util.logging)

查看:110
本文介绍了如何使用属性文件设置java日志记录? (java.util.logging的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个愚蠢的java日志记录问题:我正在从我的应用程序配置文件加载日志记录配置 - 但它只是在读取文件后没有记录任何内容(这看起来很像你会在网上找到的例子)除了额外的应用程序配置 - 删除它也没有帮助)。 初始化...日志行显得很好,但启动应用程序和任何其他消息既没有记录到控制台,也没有创建日志文件。我在这里缺少什么?

I have a stupid java logging problem: I'm loading the logging configuration from my app configuration file - but it just doesn't log anything after reading the file (which looks pretty much like the examples you will find on the net except for the additional application configuration - removing this also doesn't help). The "initializing..." log line appears just fine, but the "starting app" and any further messages are neither logged to the console, nor is the logfile ever created. What am I missing here?

Logger代码如下所示:

The Logger code looks like this:

...
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");
...

这是配置文件:

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


推荐答案

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

Okay, first intuition is here:

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

Java prop文件解析器并不是那么聪明,我不确定它会处理这个。但我会再次查看文档....

The Java prop file parser isn't all that smart, I'm not sure it'll handle this. But I'll go look at the docs again....

同时尝试:

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






更新


Update

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

No, duh, needed more coffee. Nevermind.

虽然我想更多,但请注意你可以使用属性加载和打印一个prop文件:可能值得编写一个最小程序来查看java认为它读取的内容文件。

While I think more, note that you can use the methods in Properties to load and print a prop-file: it might be worth writing a minimal program to see what java thinks it reads in that file.

另一次更新

这一行:

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

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

has an extra end-paren. It won't compile. Make sure you're working with the class file you think you are.

这篇关于如何使用属性文件设置java日志记录? (java.util.logging的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆