Java : Level (logging) - API Examples

Level (Java SE 17 & JDK 17) API Examples.
You will find code examples on most Level methods.


Class diagram

Class diagram

Fields

static final Level ALL

ALL indicates that all messages should be logged.

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

CONFIG is a message level for static configuration messages.

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

FINE is a message level providing tracing information.

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

FINER indicates a fairly detailed tracing message.

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

FINEST indicates a highly detailed tracing message.

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

INFO is a message level for informational messages.

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

OFF is a special level that can be used to turn off logging.

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

SEVERE is a message level indicating a serious failure.

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

WARNING is a message level indicating a potential problem.

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)

Create a named Level with a given integer 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)

Create a named Level with a given integer value and a given localization resource name.

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)

Compare two objects for value equality.

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 ()

Return the localized string name of the Level, for the current default locale.

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 ()

Return the non-localized string name of the Level.

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 ()

Return the level's localization resource bundle name, or null if no localization bundle is defined.

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 ()

Generate a 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 ()

Get the integer value for this level.

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)

Parse a level name string into a Level.

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 ()

Returns a string representation of this Level.

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

Related posts

To top of page