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:
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..level
is NOT working. I've tried changing it toinfo
,finest
,warning
, etc. but after I change it, I still see all the INFO messages even if I told it not to show INFO.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);
}
}