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代码如下所示:
...
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
5 个解决方案
#1
23
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 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.
虽然我想更多,但请注意,您可以使用属性中的方法来加载和打印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.
有一个额外的终端。它不会编译。确保您使用的是您认为自己的类文件。
#2
82
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.
这种方式看起来更清洁,更容易维护。
#3
12
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();
}
}
#4
5
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。
#5
3
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) .
我已经尝试了您发布的示例代码,并且在指定配置文件路径(通过代码或java args的logging.properties)后它可以正常工作。
#1
23
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 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.
虽然我想更多,但请注意,您可以使用属性中的方法来加载和打印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.
有一个额外的终端。它不会编译。确保您使用的是您认为自己的类文件。
#2
82
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.
这种方式看起来更清洁,更容易维护。
#3
12
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();
}
}
#4
5
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。
#5
3
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) .
我已经尝试了您发布的示例代码,并且在指定配置文件路径(通过代码或java args的logging.properties)后它可以正常工作。