Kayvar Kayvar - 2 months ago 10
Java Question

Java Logger: Can't set log level in logger.properties file

I have a java project that I use maven to build.
I am using

java.util.logging.Logger
in my project and would like to configure it using a
logger.properties
file (not command line).

I created a
logger.properties
file like this:

handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=WARNING

java.util.logging.SimpleFormatter.format=[%4$s] %5$s %n

java.util.logging.config.file="../../results/api.log"


Here are the problems that I'm having:


  1. java.util.logging.SimpleFormatter.format
    is WORKING
    . When I change the format here, I see the change immediately in my project. So I know that at least I am importing the file correctly.

  2. .level
    is NOT working
    . I've tried changing it to
    info
    ,
    finest
    ,
    warning
    , etc. but after I change it, I still see all the INFO messages even if I told it not to show INFO.

  3. java.util.logging.config.file
    is NOT working
    . I kind of expected this one not to work because it is a relative path. Anyone know how I can resolve the relative path name in the properties file?






Solution



I needed to move the
.level
to the top, like this:

logger.properties

.level=WARNING

handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler

java.util.logging.SimpleFormatter.format=[%4$s] %5$s %n


Then, in order to save results to "api.log", I did this in my code:

RequestHelper.java

import org.apache.commons.io.FilenameUtils;

public class RequestHelper {

private static final Logger LOGGER = Logger.getLogger( RequestHelper.class.getName() );

protected RequestHelper() {
//SET LOGGER TO OUTPUT TO "../../results/api.log"
//Logs will ALSO output to console.
String result_file = getNormalizedAbsolutePath("../../results/api.log");
Handler fh = new FileHandler(result_file);
Logger.getLogger("").addHandler(fh);
}

public static String getNormalizedAbsolutePath(String fileName) {
String path;
File file = new File(fileName);
try {
path = file.getCanonicalPath();
} catch (IOException e) {
LOGGER.warning("Error while computing the canonical path of file: " + fileName);
path = file.getAbsolutePath();
}
return FilenameUtils.normalize(path);
}
}

Answer

Just a guess. From the documentation:

All properties whose names end with ".level" are assumed to define log levels for Loggers. Thus "foo.level" defines a log level for the logger called "foo" and (recursively) for any of its children in the naming hierarchy. Log Levels are applied in the order they are defined in the properties file. Thus level settings for child nodes in the tree should come after settings for their parents.

Try setting .level first, before you define any of the handlers. If you set the root logger level first, the loggers you define afterwards will inherit the root log level.

Also:

A property "config". This property is intended to allow arbitrary configuration code to be run.

java.util.logging.config.file is a system property, and won't work in the config file. Try just using "config".