Java : Level (logging) with Examples
Level (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Level methods.
Summary
// An example of Level.FINE.
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.FINE);
final var handler = new ConsoleHandler();
handler.setLevel(Level.FINE);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.fine("FINE LOG!");
// Result
// ↓
//Jan 07, 2023 10:36:13 PM com.example.logging.LevelEnTest$1 method
//SEVERE: SEVERE LOG!
//Jan 07, 2023 10:36:13 PM com.example.logging.LevelEnTest$1 method
//WARNING: WARNING LOG!
//Jan 07, 2023 10:36:13 PM com.example.logging.LevelEnTest$1 method
//FINE: FINE LOG!
// An example of Level.SEVERE.
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.SEVERE);
final var handler = new ConsoleHandler();
handler.setLevel(Level.SEVERE);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.fine("FINE LOG!");
// Result
// ↓
//Jan 07, 2023 10:38:03 PM com.example.logging.LevelEnTest$2 method
//SEVERE: SEVERE LOG!
Fields
static final Level ALL
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
final var handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:00:47 PM com.example.logging.LevelTest$4 method
//SEVERE: SEVERE LOG!
//Mar 09, 2022 8:00:47 PM com.example.logging.LevelTest$4 method
//WARNING: WARNING LOG!
//Mar 09, 2022 8:00:47 PM com.example.logging.LevelTest$4 method
//INFO: INFO LOG!
//Mar 09, 2022 8:00:47 PM com.example.logging.LevelTest$4 method
//CONFIG: CONFIG LOG!
//Mar 09, 2022 8:00:47 PM com.example.logging.LevelTest$4 method
//FINE: FINE LOG!
//Mar 09, 2022 8:00:47 PM com.example.logging.LevelTest$4 method
//FINER: FINER LOG!
//Mar 09, 2022 8:00:47 PM com.example.logging.LevelTest$4 method
//FINEST: FINEST LOG!
static final Level CONFIG
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.CONFIG);
final var handler = new ConsoleHandler();
handler.setLevel(Level.CONFIG);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:17:57 PM com.example.logging.LevelTest$5 method
//SEVERE: SEVERE LOG!
//Mar 09, 2022 8:17:57 PM com.example.logging.LevelTest$5 method
//WARNING: WARNING LOG!
//Mar 09, 2022 8:17:57 PM com.example.logging.LevelTest$5 method
//INFO: INFO LOG!
//Mar 09, 2022 8:17:57 PM com.example.logging.LevelTest$5 method
//CONFIG: CONFIG LOG!
static final Level FINE
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.FINE);
final var handler = new ConsoleHandler();
handler.setLevel(Level.FINE);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:19:52 PM com.example.logging.LevelTest$6 method
//SEVERE: SEVERE LOG!
//Mar 09, 2022 8:19:53 PM com.example.logging.LevelTest$6 method
//WARNING: WARNING LOG!
//Mar 09, 2022 8:19:53 PM com.example.logging.LevelTest$6 method
//INFO: INFO LOG!
//Mar 09, 2022 8:19:53 PM com.example.logging.LevelTest$6 method
//CONFIG: CONFIG LOG!
//Mar 09, 2022 8:19:53 PM com.example.logging.LevelTest$6 method
//FINE: FINE LOG!
static final Level FINER
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.FINER);
final var handler = new ConsoleHandler();
handler.setLevel(Level.FINER);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:21:19 PM com.example.logging.LevelTest$7 method
//SEVERE: SEVERE LOG!
//Mar 09, 2022 8:21:19 PM com.example.logging.LevelTest$7 method
//WARNING: WARNING LOG!
//Mar 09, 2022 8:21:19 PM com.example.logging.LevelTest$7 method
//INFO: INFO LOG!
//Mar 09, 2022 8:21:19 PM com.example.logging.LevelTest$7 method
//CONFIG: CONFIG LOG!
//Mar 09, 2022 8:21:19 PM com.example.logging.LevelTest$7 method
//FINE: FINE LOG!
//Mar 09, 2022 8:21:19 PM com.example.logging.LevelTest$7 method
//FINER: FINER LOG!
static final Level FINEST
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.FINEST);
final var handler = new ConsoleHandler();
handler.setLevel(Level.FINEST);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:23:12 PM com.example.logging.LevelTest$8 method
//SEVERE: SEVERE LOG!
//Mar 09, 2022 8:23:12 PM com.example.logging.LevelTest$8 method
//WARNING: WARNING LOG!
//Mar 09, 2022 8:23:12 PM com.example.logging.LevelTest$8 method
//INFO: INFO LOG!
//Mar 09, 2022 8:23:12 PM com.example.logging.LevelTest$8 method
//CONFIG: CONFIG LOG!
//Mar 09, 2022 8:23:12 PM com.example.logging.LevelTest$8 method
//FINE: FINE LOG!
//Mar 09, 2022 8:23:12 PM com.example.logging.LevelTest$8 method
//FINER: FINER LOG!
//Mar 09, 2022 8:23:12 PM com.example.logging.LevelTest$8 method
//FINEST: FINEST LOG!
static final Level INFO
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.INFO);
final var handler = new ConsoleHandler();
handler.setLevel(Level.INFO);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:24:45 PM com.example.logging.LevelTest$9 method
//SEVERE: SEVERE LOG!
//Mar 09, 2022 8:24:45 PM com.example.logging.LevelTest$9 method
//WARNING: WARNING LOG!
//Mar 09, 2022 8:24:45 PM com.example.logging.LevelTest$9 method
//INFO: INFO LOG!
static final Level OFF
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.OFF);
final var handler = new ConsoleHandler();
handler.setLevel(Level.OFF);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//<No output>
static final Level SEVERE
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.SEVERE);
final var handler = new ConsoleHandler();
handler.setLevel(Level.SEVERE);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:27:36 PM com.example.logging.LevelTest$11 method
//SEVERE: SEVERE LOG!
static final Level WARNING
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.WARNING);
final var handler = new ConsoleHandler();
handler.setLevel(Level.WARNING);
logger.addHandler(handler);
logger.severe("SEVERE LOG!");
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.config("CONFIG LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
logger.finest("FINEST LOG!");
// Result
// ↓
//Mar 09, 2022 8:31:23 PM com.example.logging.LevelTest$12 method
//SEVERE: SEVERE LOG!
//Mar 09, 2022 8:31:23 PM com.example.logging.LevelTest$12 method
//WARNING: WARNING LOG!
Constructors
Level (String name, int value)
System.out.println(Level.INFO.intValue()); // 800
System.out.println(Level.FINE.intValue()); // 500
class SampleLevel extends Level {
SampleLevel() {
super("SAMPLE", 600);
}
}
final var sampleLevel = new SampleLevel();
System.out.println(sampleLevel.getName()); // SAMPLE
System.out.println(sampleLevel.intValue()); // 600
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(sampleLevel);
final var handler = new ConsoleHandler();
handler.setLevel(sampleLevel);
logger.addHandler(handler);
logger.info("INFO LOG!");
logger.log(sampleLevel, "SAMPLE LOG!");
logger.fine("FINE LOG!");
// Result
// ↓
//Mar 09, 2022 8:38:34 PM com.example.logging.LevelEnTest$1 method
//INFO: INFO LOG!
//Mar 09, 2022 8:38:34 PM com.example.logging.LevelEnTest$1 method
//SAMPLE: SAMPLE LOG!
Level (String name, int value, String resourceBundleName)
package com.example.logging;
import java.util.ListResourceBundle;
public class LevelEnResourceBundle extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][]{
{"SAMPLE", "XXX"}
};
}
}
System.out.println(Level.INFO.intValue()); // 800
System.out.println(Level.FINE.intValue()); // 500
class SampleLevel extends Level {
SampleLevel() {
super("SAMPLE", 600, "com.example.logging.LevelEnResourceBundle");
}
}
final var sampleLevel = new SampleLevel();
System.out.println(sampleLevel.getName()); // SAMPLE
System.out.println(sampleLevel.intValue()); // 600
// com.example.logging.LevelEnResourceBundle
System.out.println(sampleLevel.getResourceBundleName());
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(sampleLevel);
final var handler = new ConsoleHandler();
handler.setLevel(sampleLevel);
logger.addHandler(handler);
logger.info("INFO LOG!");
logger.log(sampleLevel, "SAMPLE LOG!");
logger.fine("FINE LOG!");
// Result
// ↓
//Mar 09, 2022 8:46:34 PM com.example.logging.LevelEnTest$2 method
//INFO: INFO LOG!
//Mar 09, 2022 8:46:34 PM com.example.logging.LevelEnTest$2 method
//XXX: SAMPLE LOG!
Methods
boolean equals (Object ox)
System.out.println(Level.FINE.equals(Level.FINE)); // true
System.out.println(Level.FINE.equals(Level.INFO)); // false
System.out.println(Level.WARNING.equals(Level.FINE)); // false
String getLocalizedName ()
System.out.println(Locale.getDefault()); // en_US
System.out.println(Level.OFF.getLocalizedName()); // OFF
System.out.println(Level.ALL.getLocalizedName()); // ALL
System.out.println(Level.SEVERE.getLocalizedName()); // SEVERE
System.out.println(Level.WARNING.getLocalizedName()); // WARNING
System.out.println(Level.INFO.getLocalizedName()); // INFO
System.out.println(Level.CONFIG.getLocalizedName()); // CONFIG
System.out.println(Level.FINE.getLocalizedName()); // FINE
System.out.println(Level.FINER.getLocalizedName()); // FINER
System.out.println(Level.FINEST.getLocalizedName()); // FINEST
Locale.setDefault(Locale.JAPAN);
System.out.println(Locale.getDefault()); // ja_JP
System.out.println(Level.OFF.getLocalizedName()); // オフ
System.out.println(Level.ALL.getLocalizedName()); // すべて
System.out.println(Level.SEVERE.getLocalizedName()); // 重大
String getName ()
System.out.println(Level.OFF.getName()); // OFF
System.out.println(Level.ALL.getName()); // ALL
System.out.println(Level.SEVERE.getName()); // SEVERE
System.out.println(Level.WARNING.getName()); // WARNING
System.out.println(Level.INFO.getName()); // INFO
System.out.println(Level.CONFIG.getName()); // CONFIG
System.out.println(Level.FINE.getName()); // FINE
System.out.println(Level.FINER.getName()); // FINER
System.out.println(Level.FINEST.getName()); // FINEST
String getResourceBundleName ()
final var ret = Level.SEVERE.getResourceBundleName();
System.out.println(ret); // sun.util.logging.resources.logging
class SampleLevel extends Level {
SampleLevel() {
super("SAMPLE", 600, "com.example.logging.LevelResourceBundle");
}
}
final var sampleLevel = new SampleLevel();
final var ret = sampleLevel.getResourceBundleName();
System.out.println(ret); // com.example.logging.LevelResourceBundle
int hashCode ()
System.out.println(Level.SEVERE.hashCode()); // 1000
System.out.println(Level.INFO.hashCode()); // 800
System.out.println(Level.FINE.hashCode()); // 500
final int intValue ()
System.out.println(Level.OFF.intValue()); // 2147483647 (Integer.MAX_VALUE)
System.out.println(Level.ALL.intValue()); // -2147483648 (Integer.MIN_VALUE)
System.out.println(Level.SEVERE.intValue()); // 1000
System.out.println(Level.WARNING.intValue()); // 900
System.out.println(Level.INFO.intValue()); // 800
System.out.println(Level.CONFIG.intValue()); // 700
System.out.println(Level.FINE.intValue()); // 500
System.out.println(Level.FINER.intValue()); // 400
System.out.println(Level.FINEST.intValue()); // 300
static Level parse (String name)
final var levelA = Level.parse("INFO");
System.out.println(levelA.getName()); // "INFO"
System.out.println(levelA.intValue()); // 800
final var levelB = Level.parse("800");
System.out.println(levelB.getName()); // "INFO"
System.out.println(levelB.intValue()); // 800
// Undefined value.
final var level = Level.parse("123");
System.out.println(level.getName()); // "123"
System.out.println(level.intValue()); // 123
// Undefined name.
//Level.parse("AAA"); // IllegalArgumentException: Bad level "AAA"
final String toString ()
final var ret1 = Level.SEVERE.toString();
System.out.println(ret1); // SEVERE
final var ret2 = Level.INFO.toString();
System.out.println(ret2); // INFO
final var ret3 = Level.CONFIG.toString();
System.out.println(ret3); // CONFIG