How to set up java logging using a properties file? (java.util.logging)
Asked Answered
H

6

92

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?

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
Hoon answered 6/6, 2009 at 16:52 Comment(0)
P
30

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.

Persuasive answered 6/6, 2009 at 16:58 Comment(4)
Well, it does seam to have something to do with the readConfiguration line - I stepped through this with a debugger and all the properties of the LogManager are cleared after this call.Hoon
Oh yes, got it - I'm using the same input stream twice, so I need to reposition it using configFile.reset() - otherwise the loadConfiguration() call will have nothing to read. Btw. the ) was just a copying error from my working code.Hoon
I am not quite sure what's the answer here?Retrogression
@OndraŽižka the answer is "your code didn't work because of a syntax error."Persuasive
P
114

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.

Pocket answered 6/6, 2009 at 17:16 Comment(2)
Yes, I'll probably add a check if this property is set and let it overwrite my config - having this all in one configuration file which works out-of-the box would be nice, though. Thanks!Hoon
this will only work locally though, once you deploy you need the properties file as as streamMoray
P
30

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.

Persuasive answered 6/6, 2009 at 16:58 Comment(4)
Well, it does seam to have something to do with the readConfiguration line - I stepped through this with a debugger and all the properties of the LogManager are cleared after this call.Hoon
Oh yes, got it - I'm using the same input stream twice, so I need to reposition it using configFile.reset() - otherwise the loadConfiguration() call will have nothing to read. Btw. the ) was just a copying error from my working code.Hoon
I am not quite sure what's the answer here?Retrogression
@OndraŽižka the answer is "your code didn't work because of a syntax error."Persuasive
L
14

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");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        fis.close();
    }
}
Lapillus answered 20/9, 2010 at 9:30 Comment(2)
where should be the p.properties in relation to main class?Nativeborn
You should close the stream in the finally block, or use a try-with-resource.Mersey
R
9
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().

Rici answered 5/1, 2016 at 6:58 Comment(0)
S
3

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

Swindle answered 17/8, 2009 at 10:33 Comment(1)
Sir, may I know where is the online documentation that state %h resolves to my home, %u resolve to something else ,etc.Entitle
U
0

java.util.logging.config.file is a file system path, which is less convenient than classpath in Java world.

There is java.util.logging.config.class option that specifies initialization class with a no-arg constructor, which will be on classpath...

This way your initialization logic is outside of main() method and you could search for a properties file in classpath:

try (Inputstream stream : JulBootstrap.class.getResourceAsStream("/logging.properties")){
    LogManager.readConfiguration(stream);
}

Other examples are here: Setting java.util.logging.config.file at runtime

Urus answered 3/10, 2023 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.