In this example, we will learn how to change Java Util Logging default level to a new value.
To change a log level we must use Logger#setLevel() and Handler#setLevel() .
Examples
Programmatically setting Log Level
We are going to set the log Level to ALL on the root logger/handlers, then we will find out all declared Levels in the Level class via reflection and then we will use the log() method on them. We will also set a custom easy to read log format.
public class LogLevelExample {
private static Logger log = Logger.getLogger(LogLevelExample.class.getName());
static {
System.setProperty("java.util.logging.SimpleFormatter.format",
"[%1$tF %1$tT %1$tL] [%4$-7s] %5$s %n");
}
public static void main(String[] args) throws Exception {
setLevel(Level.ALL);
Set<Level> levels = getAllLevels();
int i = 1;
for (Level level : levels) {
log.log(level, level.getName() + " - " + (i++));
}
}
public static void setLevel(Level targetLevel) {
Logger root = Logger.getLogger("");
root.setLevel(targetLevel);
for (Handler handler : root.getHandlers()) {
handler.setLevel(targetLevel);
}
System.out.println("level set: " + targetLevel.getName());
}
public static Set<Level> getAllLevels() throws IllegalAccessException {
Class<Level> levelClass = Level.class;
Set<Level> allLevels = new TreeSet<>(
Comparator.comparingInt(Level::intValue));
for (Field field : levelClass.getDeclaredFields()) {
if (field.getType() == Level.class) {
allLevels.add((Level) field.get(null));
}
}
return allLevels;
}
} Outputlevel set: ALL [2017-09-21 13:55:24 796] [ALL ] ALL - 1 [2017-09-21 13:55:24 798] [FINEST ] FINEST - 2 [2017-09-21 13:55:24 799] [FINER ] FINER - 3 [2017-09-21 13:55:24 799] [FINE ] FINE - 4 [2017-09-21 13:55:24 799] [CONFIG ] CONFIG - 5 [2017-09-21 13:55:24 799] [INFO ] INFO - 6 [2017-09-21 13:55:24 800] [SEVERE ] SEVERE - 8 [2017-09-21 13:55:24 800] [OFF ] OFF - 9
Setting Level in properties file
We are going to set FINEST level:
src/main/resources/logging.propertieshandlers= java.util.logging.ConsoleHandler
.level= FINEST
java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$-7s] %5$s %n
public class LogLevelPropertiesExample {
private static Logger log;
static {
String path = LogLevelPropertiesExample.class
.getClassLoader()
.getResource("logging.properties")
.getFile();
System.setProperty("java.util.logging.config.file", path);
log = Logger.getLogger(LogLevelPropertiesExample.class.getName());
}
public static void main(String[] args) throws Exception {
Set<Level> levels = getAllLevels();
int i = 1;
for (Level level : levels) {
log.log(level, level.getName() + " - " + (i++));
}
}
.............
} Output[2017-09-20 23:49:20] [FINEST ] FINEST - 2 [2017-09-20 23:49:20] [FINER ] FINER - 3 [2017-09-20 23:49:20] [FINE ] FINE - 4 [2017-09-20 23:49:20] [CONFIG ] CONFIG - 5 [2017-09-20 23:49:20] [INFO ] INFO - 6 [2017-09-20 23:49:20] [WARNING] WARNING - 7 [2017-09-20 23:49:20] [SEVERE ] SEVERE - 8 [2017-09-20 23:49:20] [OFF ] OFF - 9
The default log Level
The default level is INFO. If we don't set the level in either of above examples then output would be:
[2017-09-20 23:51:45 801] [INFO ] INFO - 6
[2017-09-20 23:51:45 834] [WARNING] WARNING - 7
[2017-09-20 23:51:45 834] [SEVERE ] SEVERE - 8
[2017-09-20 23:51:45 834] [OFF ] OFF - 9
Example ProjectDependencies and Technologies Used:
|