Controlling Logging Levels through Namespaces
Although not mandatory, it’s advisable to give a logger the name of the class in which it is used. This allows you to manipulate the logging level of groups of loggers that reside in the same package hierarchy, at the granularity of the directory package structure. For example, you can modify all the logging levels of all the packages in com, or just the ones in com.bruceeckel, or just the ones in com.bruceeckel.util, as shown in the following example:
//: c15:LoggingLevelManipulation.java
import com.bruceeckel.simpletest.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.LogManager;
public class LoggingLevelManipulation {
private static Test monitor = new Test();
private static Logger
lgr = Logger.getLogger("com"),
lgr2 = Logger.getLogger("com.bruceeckel"),
util = Logger.getLogger("com.bruceeckel.util"),
test = Logger.getLogger("com.bruceeckel.test"),
rand = Logger.getLogger("random");
static void printLogMessages(Logger logger) {
logger.finest(logger.getName() + " Finest");
logger.finer(logger.getName() + " Finer");
logger.fine(logger.getName() + " Fine");
logger.config(logger.getName() + " Config");
logger.info(logger.getName() + " Info");
logger.warning(logger.getName() + " Warning");
logger.severe(logger.getName() + " Severe");
}
static void logMessages() {
printLogMessages(lgr);
printLogMessages(lgr2);
printLogMessages(util);
printLogMessages(test);
printLogMessages(rand);
}
static void printLevels() {
System.out.println(" -- printing levels -- "
+ lgr.getName() + " : " + lgr.getLevel()
+ " " + lgr2.getName() + " : " + lgr2.getLevel()
+ " " + util.getName() + " : " + util.getLevel()
+ " " + test.getName() + " : " + test.getLevel()
+ " " + rand.getName() + " : " + rand.getLevel());
}
public static void main(String[] args) {
printLevels();
lgr.setLevel(Level.SEVERE);
printLevels();
System.out.println("com level: SEVERE");
logMessages();
util.setLevel(Level.FINEST);
test.setLevel(Level.FINEST);
rand.setLevel(Level.FINEST);
printLevels();
System.out.println(
"individual loggers set to FINEST");
logMessages();
lgr.setLevel(Level.FINEST);
printLevels();
System.out.println("com level: FINEST");
logMessages();
monitor.expect("LoggingLevelManipulation.out");
}
} ///:~
As you can see in this code, if you pass getLogger( ) a string representing a namespace, the resulting Logger will control the severity levels of that namespace; that is, all the packages within that namespace will be affected by changes to the severity level of the logger.
Each Logger keeps a track of its existing ancestor Logger. If a child logger already has a logging level set, then that level is used instead of the parent's logging level. Changing the logging level of the parent does not affect the logging level of the child once the child has its own logging level.
Although the level of individual loggers is set to FINEST, only messages with a logging level equal to or more severe than INFO are printed because we are using the ConsoleHandler of the root logger, which is at INFO.
Because it isn’t in the same namespace, the logging level of random remains unaffected when the logging level of the logger com or com.bruceeckel is changed.