如何使用属性文件设置 java 日志记录

我有一个愚蠢的 java 日志记录问题: 我正在从我的应用配置文件中加载日志配置——但是它只是在读取文件后没有记录任何东西(除了额外的应用配置之外,它看起来非常像你在网上找到的例子——删除它也没有帮助)。“ initialization...”日志行看起来很好,但是“ start app”和任何进一步的消息都不会记录到控制台,也不会创建日志文件。我错过了什么?

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
276834 次浏览

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....

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.

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.

you can set your logging configuration file through command line:

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

this way seems cleaner and easier to maintain.

Are you searching for the log file in the right path: %h/one%u.log

Here %h resolves to your home : In windows this defaults to : 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) .

I have tried your code in above code don't use [preferences.load(configFile);] statement and it will work. Here is a running sample code

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();
}
}
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().