如何使用属性文件设置 Java 日志记录?(java.util.logging)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/960099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up java logging using a properties file? (java.util.logging)
提问by VolkA
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?
我有一个愚蠢的 java 日志记录问题:我正在从我的应用程序配置文件加载日志记录配置 - 但它在读取文件后没有记录任何内容(这看起来很像你会在网上找到的例子,除了额外的应用程序配置 - 删除它也无济于事)。“正在初始化...”日志行看起来很好,但“正在启动的应用程序”和任何进一步的消息既没有记录到控制台,也没有创建日志文件。我在这里缺少什么?
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");
...
And this is the configuration file:
这是配置文件:
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
采纳答案by Charlie Martin
Okay, first intuition is here:
好的,第一直觉在这里:
handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level = ALL
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....
Java prop 文件解析器并不是那么聪明,我不确定它会处理这个问题。但我会再去看看文档....
In the mean time, try:
同时,尝试:
handlers = java.util.logging.FileHandler
java.util.logging.ConsoleHandler.level = ALL
Update
更新
No, duh, needed more coffee. Nevermind.
不,呃,需要更多的咖啡。没关系。
While I think more, note that you can use the methods in Propertiesto load and print a prop-file: it might be worth writing a minimal program to see what java thinks it reads in that file.
虽然我想得更多,但请注意,您可以使用Properties 中的方法来加载和打印一个 prop 文件:编写一个最小的程序来查看 java 认为它在该文件中读取的内容可能是值得的。
Another update
另一个更新
This line:
这一行:
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.
有一个额外的结束括号。它不会编译。确保您正在使用您认为的类文件。
回答by cd1
you can set your logging configuration file through command line:
您可以通过命令行设置日志记录配置文件:
$ java -Djava.util.logging.config.file=/path/to/app.properties MainClass
$ java -Djava.util.logging.config.file=/path/to/app.properties MainClass
this way seems cleaner and easier to maintain.
这种方式看起来更干净,更容易维护。
回答by techzen
Are you searching for the log file in the right path: %h/one%u.log
您是否在正确的路径中搜索日志文件:%h/one%u.log
Here %h resolves to your home : In windows this defaults to : C:\Documents and Settings(user_name).
此处 %h 解析为您的家:在 Windows 中,默认为:C:\Documents and Settings(user_name)。
I have tried the sample code you have posted and it works fine after you specify the configuration file path (logging.properties either through code or java args) .
我已经尝试了您发布的示例代码,并且在您指定配置文件路径(logging.properties 通过代码或 java args)后它工作正常。
回答by Rana
I have tried your code in above code don't use [preferences.load(configFile);] statement and it will work.here is running sample code
我已经在上面的代码中尝试过你的代码不要使用 [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();
}
}
回答by Vikky Kumar
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");
this is working..:) you have to pass InputStream in readConfiguration().
这是有效的..:) 你必须在 readConfiguration() 中传递 InputStream。