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

查看:26
本文介绍了如何使用属性文件设置 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


更新

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

No, duh, needed more coffee. Nevermind.

虽然我想得更多,但请注意,您可以使用 Properties 加载和打印 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天全站免登陆