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

Vol*_*lkA 78 java logging

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

Logger代码如下所示:

...
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");
...
Run Code Online (Sandbox Code Playgroud)

这是配置文件:

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
Run Code Online (Sandbox Code Playgroud)

cd1*_*cd1 90

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

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

这种方式看起来更清洁,更容易维护.


Cha*_*tin 26

好的,首先是直觉在这里:

handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level = ALL
Run Code Online (Sandbox Code Playgroud)

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

同时,尝试:

handlers = java.util.logging.FileHandler
java.util.logging.ConsoleHandler.level = ALL
Run Code Online (Sandbox Code Playgroud)

更新

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

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


另一个更新

这一行:

    FileInputStream configFile = new FileInputStream("/path/to/app.properties"));
Run Code Online (Sandbox Code Playgroud)

有一个额外的终端.它不会编译.确保您正在使用您认为自己的类文件.

  • 哦,是的,得到它 - 我使用相同的输入流两次,所以我需要使用configFile.reset()重新定位它 - 否则loadConfiguration()调用将没有任何东西可读.顺便说一句.这只是我工作代码中的复制错误. (5认同)
  • 我不太确定这里的答案是什么? (2认同)

小智 12

我在上面的代码中尝试过你的代码,不要使用[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();
    }
}
Run Code Online (Sandbox Code Playgroud)


Vik*_*mar 8

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");
Run Code Online (Sandbox Code Playgroud)

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