我有一个愚蠢的 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");
...
这是配置文件:
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
最佳答案
您可以通过命令行设置日志记录配置文件:
$ java -Djava.util.logging.config.file=/path/to/app.properties MainClass
这种方式看起来更干净,更容易维护。
关于java - 如何使用属性文件设置 java 日志记录? (java.util.logging),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/960099/