Configure java.util.logging at run-time

I recently wanted to change a few logging levels programmatically, i.e. without having to modify my system-wide properties (see e.g. Java Logging for Lazy Dogs) or starting the JVM with an extra option like: java -Djava.util.logging.config.file=~/logging.properties.

My first impulse was to google for a ready-made snippet of code. The results weren't exactly encouraging. I found a number of claims that you could not change the logging settings at run-time and numerous examples how to do one of the options mentioned above. So I decided to read the java.util.logging Javadocs instead. Is there anybody else that thinks the java.util.logging Javadocs are not particularly helpful, no matter what you are looking for? Anyway, this is the solution I finally came up with:

 

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;

try {
System.setProperty("java.util.logging.config.file", "logging.properties");
LogManager logManager = LogManager.getLogManager();
logManager.readConfiguration();
} catch (IOException e) {
throw new MyException("Failed to load file logging.properties", e);
}

 

This code sets the name of a configuration file that will be read from your working directory and then reinitializes all loggers.

Technorati:

Comments:

Post a Comment:
Comments are closed for this entry.
About

ritzmann

Search

Categories
Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today