Java : Logger (logging) with Examples

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


Summary

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.swing.

Class diagram

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
logger.addHandler(handler);

logger.setLevel(Level.ALL);
handler.setLevel(Level.ALL);

logger.log(Level.INFO, "INFO LOG!");

logger.severe("SEVERE LOG!");
logger.fine("FINE LOG!");

// Result
// ↓
//Mar 12, 2022 7:56:13 PM com.example.logging.LoggerTest$1 method
//INFO: INFO LOG!
//Mar 12, 2022 7:56:13 PM com.example.logging.LoggerTest$1 method
//SEVERE: SEVERE LOG!
//Mar 12, 2022 7:56:13 PM com.example.logging.LoggerTest$1 method
//FINE: FINE LOG!

Fields

static final Logger global

Deprecated. Initialization of this field is prone to deadlocks.

Deprecated.

static final String GLOBAL_LOGGER_NAME

GLOBAL_LOGGER_NAME is a name for the global logger.

System.out.println(Logger.GLOBAL_LOGGER_NAME); // global

final var logger = Logger.getGlobal();
System.out.println(logger.getName()); // global

Constructors

Logger (String name, String resourceBundleName)

Protected method to construct a logger for a named subsystem.

Normally, you don't need to create a subclass. If create the subclass, please see Subclassing Information in the Logger API spec.

Methods

void addHandler (Handler handler)

Add a log Handler to receive logging messages.

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

// []
System.out.println(Arrays.toString(logger.getHandlers()));

logger.info("TEST LOG : 1");

// Result
// ↓
//<No output>

final var handler = new ConsoleHandler();
System.out.println(handler.getLevel()); // INFO

logger.addHandler(handler);

// [java.util.logging.ConsoleHandler@46d59067]
System.out.println(Arrays.toString(logger.getHandlers()));

logger.info("TEST LOG : 2");

// Result
// ↓
//Mar 12, 2022 1:38:02 AM com.example.logging.LoggerTest$3 method
//INFO: TEST LOG : 2
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

final var pattern = Path.of("R:", "java-work", "log.txt");
logger.addHandler(new FileHandler(pattern.toString(), true));

// [java.util.logging.ConsoleHandler@9816741, java.util.logging.FileHandler@1e16c0aa]
System.out.println(Arrays.toString(logger.getHandlers()));

logger.info("TEST LOG!");

// Result (Console)
// ↓
//Mar 12, 2022 8:08:53 PM com.example.logging.LoggerTest$4 method
//INFO: TEST LOG!

// Result (File)
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-13T04:08:53.088569700Z</date>
//  <millis>1647144533088</millis>
//  <nanos>569700</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.LoggerTest$4</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG!</message>
//</record>
//</log>

void config (String msg)

Log a CONFIG message.

Please see info(String msg).

void config (Supplier<String> msgSupplier)

Log a CONFIG message, which is only to be constructed if the logging level is such that the message will actually be logged.

Please see info(Supplier<String> msgSupplier).

void entering (String sourceClass, String sourceMethod)

Log a method entry.

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

class EntryTest {
    public void func() {
        try {
            logger.entering("EntryTest", "func");
        } finally {
            logger.exiting("EntryTest", "func");
        }
    }
}

new EntryTest().func();

// Result
// ↓
//Mar 12, 2022 8:12:15 PM EntryTest func
//FINER: ENTRY
//Mar 12, 2022 8:12:15 PM EntryTest func
//FINER: RETURN

void entering (String sourceClass, String sourceMethod, Object param1)

Log a method entry, with one parameter.

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

class EntryTest {
    public void func() {
        try {
            logger.entering("EntryTest", "func", "Param!");
        } finally {
            logger.exiting("EntryTest", "func", "Result!");
        }
    }
}

new EntryTest().func();

// Result
// ↓
//Mar 12, 2022 8:17:43 PM EntryTest func
//FINER: ENTRY Param!
//Mar 12, 2022 8:17:43 PM EntryTest func
//FINER: RETURN Result!

void entering (String sourceClass, String sourceMethod, Object[] params)

Log a method entry, with an array of parameters.

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

class EntryTest {
    public void func() {
        try {
            logger.entering("EntryTest", "func",
                    new String[]{"Param A", "Param B", "Param C"});
        } finally {
            logger.exiting("EntryTest", "func", "Result!");
        }
    }
}

new EntryTest().func();

// Result
// ↓
//Mar 12, 2022 8:19:43 PM EntryTest func
//FINER: ENTRY Param A Param B Param C
//Mar 12, 2022 8:19:43 PM EntryTest func
//FINER: RETURN Result!

void exiting (String sourceClass, String sourceMethod)

Log a method return.

Please see entering(String sourceClass, String sourceMethod).

void exiting (String sourceClass, String sourceMethod, Object result)

Log a method return, with result object.

Please see entering(String sourceClass, String sourceMethod, Object param1).

void fine (String msg)

Log a FINE message.

Please see info(String msg).

void fine (Supplier<String> msgSupplier)

Log a FINE message, which is only to be constructed if the logging level is such that the message will actually be logged.

Please see info(Supplier<String> msgSupplier).

void finer (String msg)

Log a FINER message.

Please see info(String msg).

void finer (Supplier<String> msgSupplier)

Log a FINER message, which is only to be constructed if the logging level is such that the message will actually be logged.

Please see info(Supplier<String> msgSupplier).

void finest (String msg)

Log a FINEST message.

Please see info(String msg).

void finest (Supplier<String> msgSupplier)

Log a FINEST message, which is only to be constructed if the logging level is such that the message will actually be logged.

Please see info(Supplier<String> msgSupplier).

static Logger getAnonymousLogger ()

Create an anonymous Logger.

final var logger = Logger.getAnonymousLogger();

System.out.println(logger.getName()); // null
System.out.println(logger.getLevel()); // null

final var parent = logger.getParent();
System.out.println(parent.getName()); // ""
System.out.println(parent.getLevel()); // INFO
final var logger = Logger.getAnonymousLogger();
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.info("TEST LOG!");

// Result
// ↓
//Mar 12, 2022 8:22:50 PM com.example.logging.LoggerTest$8 method
//INFO: TEST LOG!

static Logger getAnonymousLogger (String resourceBundleName)

Create an anonymous Logger.

package com.example.logging;

import java.util.ListResourceBundle;

public class LoggerEnResourceBundle extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][]{
                {"SAMPLE", "XXX"}
        };
    }
}
final var logger = Logger.getAnonymousLogger("com.example.logging.LoggerEnResourceBundle");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.info("SAMPLE");

// Result
// ↓
//Mar 12, 2022 8:29:25 PM com.example.logging.LoggerEnTest$1 method
//INFO: XXX

Filter getFilter ()

Get the current filter for this Logger.

Please see setFilter(Filter newFilter).

static final Logger getGlobal ()

Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.

System.out.println(Logger.GLOBAL_LOGGER_NAME); // global

final var logger = Logger.getGlobal();
System.out.println(logger.getName()); // global

Handler[] getHandlers ()

Get the Handlers associated with this logger.

final var logger = Logger.getLogger("com.example.logging");

// []
System.out.println(Arrays.toString(logger.getHandlers()));

final var consoleHandler = new ConsoleHandler();
logger.addHandler(consoleHandler);

// [java.util.logging.ConsoleHandler@29290e83]
System.out.println(Arrays.toString(logger.getHandlers()));

final var fileHandler = new FileHandler();
logger.addHandler(fileHandler);

// [java.util.logging.ConsoleHandler@29290e83, java.util.logging.FileHandler@25d9da2a]
System.out.println(Arrays.toString(logger.getHandlers()));

logger.removeHandler(consoleHandler);

// [java.util.logging.FileHandler@25d9da2a]
System.out.println(Arrays.toString(logger.getHandlers()));

logger.removeHandler(fileHandler);

// []
System.out.println(Arrays.toString(logger.getHandlers()));

Level getLevel ()

Get the log Level that has been specified for this Logger.

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

System.out.println(logger.getLevel()); // null

logger.setLevel(Level.FINE);
System.out.println(logger.getLevel()); // FINE

final var handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);

logger.info("INFO LOG!");
logger.fine("FINE LOG!");
logger.finest("FINEST LOG!");

// Result
// ↓
//Mar 12, 2022 8:33:25 PM com.example.logging.LoggerTest$10 method
//INFO: INFO LOG!
//Mar 12, 2022 8:33:25 PM com.example.logging.LoggerTest$10 method
//FINE: FINE LOG!

static Logger getLogger (String name)

Find or create a logger for a named subsystem.

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

System.out.println(logger.getName()); // com.example.logging
System.out.println(logger.getLevel()); // null

logger.addHandler(new ConsoleHandler());

logger.info("TEST LOG!");

// Result
// ↓
//Mar 12, 2022 8:35:38 PM com.example.logging.LoggerTest$11 method
//INFO: TEST LOG!

static Logger getLogger (String name, String resourceBundleName)

Find or create a logger for a named subsystem.

package com.example.logging;

import java.util.ListResourceBundle;

public class LoggerEnResourceBundle extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][]{
                {"SAMPLE", "XXX"}
        };
    }
}
final var logger = Logger.getLogger(
        "com.example.logging",
        "com.example.logging.LoggerEnResourceBundle");
logger.setUseParentHandlers(false);

// com.example.logging.LoggerEnResourceBundle
System.out.println(logger.getResourceBundleName());

logger.addHandler(new ConsoleHandler());

logger.info("SAMPLE");

// Result
// ↓
//Mar 12, 2022 8:40:22 PM com.example.logging.LoggerEnTest$2 method
//INFO: XXX

String getName ()

Get the name for this logger.

final var logger = Logger.getLogger("com.example.logging");
System.out.println(logger.getName()); // com.example.logging
System.out.println(logger.getLevel()); // null

logger.addHandler(new ConsoleHandler());
logger.setUseParentHandlers(false);

logger.info("TEST LOG!");

// Result
// ↓
//Mar 12, 2022 8:43:17 PM com.example.logging.LoggerTest$13 method
//INFO: TEST LOG!

Logger getParent ()

Return the parent for this Logger.

final var comLogger = Logger.getLogger("com");
System.out.println(comLogger.getName()); // "com"

final var comExampleLogger = Logger.getLogger("com.example");
System.out.println(comExampleLogger.getName()); // "com.example"

final var parent = comExampleLogger.getParent();
System.out.println(parent.getName()); // "com"

// The root logger (the name is empty)
final var parentParent = parent.getParent();
System.out.println(parentParent.getName()); // ""

System.out.println(parentParent.getParent()); // null

ResourceBundle getResourceBundle ()

Retrieve the localization resource bundle for this logger.

Please see setResourceBundle(ResourceBundle bundle).

String getResourceBundleName ()

Retrieve the localization resource bundle name for this logger.

Please see getLogger(String name, String resourceBundleName).

boolean getUseParentHandlers ()

Discover whether or not this logger is sending its output to its parent logger.

Please see setUseParentHandlers(boolean useParentHandlers).

void info (String msg)

Log an INFO message.

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 12, 2022 8:49:58 PM com.example.logging.LoggerTest$14 method
//SEVERE: SEVERE LOG!
//Mar 12, 2022 8:49:58 PM com.example.logging.LoggerTest$14 method
//WARNING: WARNING LOG!
//Mar 12, 2022 8:49:58 PM com.example.logging.LoggerTest$14 method
//INFO: INFO LOG!
//Mar 12, 2022 8:49:58 PM com.example.logging.LoggerTest$14 method
//CONFIG: CONFIG LOG!
//Mar 12, 2022 8:49:58 PM com.example.logging.LoggerTest$14 method
//FINE: FINE LOG!
//Mar 12, 2022 8:49:58 PM com.example.logging.LoggerTest$14 method
//FINER: FINER LOG!
//Mar 12, 2022 8:49:58 PM com.example.logging.LoggerTest$14 method
//FINEST: FINEST LOG!

void info (Supplier<String> msgSupplier)

Log a INFO message, which is only to be constructed if the logging level is such that the message will actually 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(() -> {
    return "SEVERE LOG!";
});
logger.warning(() -> {
    return "WARNING LOG!";
});
logger.info(() -> {
    return "INFO LOG!";
});
logger.config(() -> {
    return "CONFIG LOG!";
});
logger.fine(() -> {
    return "FINE LOG!";
});
logger.finer(() -> {
    return "FINER LOG!";
});
logger.finest(() -> {
    return "FINEST LOG!";
});

// Result
// ↓
//Mar 12, 2022 8:53:23 PM com.example.logging.LoggerTest$15 method
//SEVERE: SEVERE LOG!
//Mar 12, 2022 8:53:23 PM com.example.logging.LoggerTest$15 method
//WARNING: WARNING LOG!
//Mar 12, 2022 8:53:23 PM com.example.logging.LoggerTest$15 method
//INFO: INFO LOG!
//Mar 12, 2022 8:53:23 PM com.example.logging.LoggerTest$15 method
//CONFIG: CONFIG LOG!
//Mar 12, 2022 8:53:23 PM com.example.logging.LoggerTest$15 method
//FINE: FINE LOG!
//Mar 12, 2022 8:53:23 PM com.example.logging.LoggerTest$15 method
//FINER: FINER LOG!
//Mar 12, 2022 8:53:23 PM com.example.logging.LoggerTest$15 method
//FINEST: FINEST LOG!

boolean isLoggable (Level level)

Check if a message of the given level would actually be logged by this logger.

final var logger = Logger.getLogger("com.example.logging");

logger.setLevel(Level.ALL);

System.out.println(logger.isLoggable(Level.SEVERE)); // true
System.out.println(logger.isLoggable(Level.WARNING)); // true
System.out.println(logger.isLoggable(Level.INFO)); // true
System.out.println(logger.isLoggable(Level.CONFIG)); // true
System.out.println(logger.isLoggable(Level.FINE)); // true
System.out.println(logger.isLoggable(Level.FINER)); // true
System.out.println(logger.isLoggable(Level.FINEST)); // true

logger.setLevel(Level.INFO);

System.out.println(logger.isLoggable(Level.SEVERE)); // true
System.out.println(logger.isLoggable(Level.WARNING)); // true
System.out.println(logger.isLoggable(Level.INFO)); // true
System.out.println(logger.isLoggable(Level.CONFIG)); // false
System.out.println(logger.isLoggable(Level.FINE)); // false
System.out.println(logger.isLoggable(Level.FINER)); // false
System.out.println(logger.isLoggable(Level.FINEST)); // false

logger.setLevel(Level.OFF);

System.out.println(logger.isLoggable(Level.SEVERE)); // false
System.out.println(logger.isLoggable(Level.WARNING)); // false
System.out.println(logger.isLoggable(Level.INFO)); // false
System.out.println(logger.isLoggable(Level.CONFIG)); // false
System.out.println(logger.isLoggable(Level.FINE)); // false
System.out.println(logger.isLoggable(Level.FINER)); // false
System.out.println(logger.isLoggable(Level.FINEST)); // false

void log (Level level, String msg)

Log a message, with no arguments.

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.log(Level.SEVERE, "SEVERE LOG!");
logger.log(Level.WARNING, "WARNING LOG!");
logger.log(Level.INFO, "INFO LOG!");
logger.log(Level.CONFIG, "CONFIG LOG!");
logger.log(Level.FINE, "FINE LOG!");
logger.log(Level.FINER, "FINER LOG!");
logger.log(Level.FINEST, "FINEST LOG!");

// Result
// ↓
//Mar 12, 2022 9:03:05 PM com.example.logging.LoggerTest$16 method
//SEVERE: SEVERE LOG!
//Mar 12, 2022 9:03:05 PM com.example.logging.LoggerTest$16 method
//WARNING: WARNING LOG!
//Mar 12, 2022 9:03:05 PM com.example.logging.LoggerTest$16 method
//INFO: INFO LOG!
//Mar 12, 2022 9:03:05 PM com.example.logging.LoggerTest$16 method
//CONFIG: CONFIG LOG!
//Mar 12, 2022 9:03:05 PM com.example.logging.LoggerTest$16 method
//FINE: FINE LOG!
//Mar 12, 2022 9:03:05 PM com.example.logging.LoggerTest$16 method
//FINER: FINER LOG!
//Mar 12, 2022 9:03:05 PM com.example.logging.LoggerTest$16 method
//FINEST: FINEST LOG!

void log (Level level, String msg, Object param1)

Log a message, with one object parameter.

Please see also : log(Level level, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
handler.setFormatter(new XMLFormatter());
logger.addHandler(handler);

logger.log(Level.INFO, "TEST LOG!", "Param!");

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-10T07:12:46.997867Z</date>
//  <millis>1646896366997</millis>
//  <nanos>867000</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.LoggerTest$17</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG!</message>
//  <param>Param!</param>
//</record>

void log (Level level, String msg, Object[] params)

Log a message, with an array of object arguments.

Please see also : log(Level level, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
handler.setFormatter(new XMLFormatter());
logger.addHandler(handler);

logger.log(Level.INFO, "TEST LOG!",
        new String[]{"Param A", "Param B", "Param C"});

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-10T07:48:20.054755200Z</date>
//  <millis>1646898500054</millis>
//  <nanos>755200</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.LoggerTest$18</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG!</message>
//  <param>Param A</param>
//  <param>Param B</param>
//  <param>Param C</param>
//</record>

void log (Level level, String msg, Throwable thrown)

Log a message, with associated Throwable information.

Please see also : log(Level level, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.log(Level.SEVERE, "TEST LOG!", new IllegalStateException("TEST EXCEPTION!"));

//Mar 12, 2022 9:19:37 PM com.example.logging.LoggerTest$19 method
//SEVERE: TEST LOG!
//java.lang.IllegalStateException: TEST EXCEPTION!
//	at com.example.logging.LoggerTest$19.method(LoggerTest.java:1232)
//...

void log (Level level, Throwable thrown, Supplier<String> msgSupplier)

Log a lazily constructed message, with associated Throwable information.

Please see also : log(Level level, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.log(Level.SEVERE, new IllegalStateException("TEST EXCEPTION!"), () -> {
    return "TEST LOG!";
});

// Result
// ↓
//Mar 12, 2022 9:21:41 PM com.example.logging.LoggerTest$20 method
//SEVERE: TEST LOG!
//java.lang.IllegalStateException: TEST EXCEPTION!
//	at com.example.logging.LoggerTest$20.method(LoggerTest.java:1267)
//...

void log (Level level, Supplier<String> msgSupplier)

Log a message, which is only to be constructed if the logging level is such that the message will actually be logged.

Please see also : log(Level level, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.log(Level.INFO, () -> {
    return "TEST LOG!";
});

// Result
// ↓
//Mar 12, 2022 9:23:41 PM com.example.logging.LoggerTest$21 method
//INFO: TEST LOG!

void log (LogRecord record)

Log a LogRecord.

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.log(new LogRecord(Level.INFO, "TEST LOG!"));

// Result
// ↓
//Mar 12, 2022 9:27:20 PM com.example.logging.LoggerTest$22 method
//INFO: TEST LOG!

void logp (Level level, String sourceClass, String sourceMethod, String msg)

Log a message, specifying source class and method, with no arguments.

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.logp(Level.SEVERE, "SampleClass", "sampleMethod", "SEVERE LOG!");
logger.logp(Level.WARNING, "SampleClass", "sampleMethod", "WARNING LOG!");
logger.logp(Level.INFO, "SampleClass", "sampleMethod", "INFO LOG!");
logger.logp(Level.CONFIG, "SampleClass", "sampleMethod", "CONFIG LOG!");
logger.logp(Level.FINE, "SampleClass", "sampleMethod", "FINE LOG!");
logger.logp(Level.FINER, "SampleClass", "sampleMethod", "FINER LOG!");
logger.logp(Level.FINEST, "SampleClass", "sampleMethod", "FINEST LOG!");

// Result
// ↓
//Mar 12, 2022 9:28:54 PM SampleClass sampleMethod
//SEVERE: SEVERE LOG!
//Mar 12, 2022 9:28:54 PM SampleClass sampleMethod
//WARNING: WARNING LOG!
//Mar 12, 2022 9:28:54 PM SampleClass sampleMethod
//INFO: INFO LOG!
//Mar 12, 2022 9:28:54 PM SampleClass sampleMethod
//CONFIG: CONFIG LOG!
//Mar 12, 2022 9:28:54 PM SampleClass sampleMethod
//FINE: FINE LOG!
//Mar 12, 2022 9:28:54 PM SampleClass sampleMethod
//FINER: FINER LOG!
//Mar 12, 2022 9:28:54 PM SampleClass sampleMethod
//FINEST: FINEST LOG!

void logp (Level level, String sourceClass, String sourceMethod, String msg, Object param1)

Log a message, specifying source class and method, with a single object parameter to the log message.

Please see also : logp(Level level, String sourceClass, String sourceMethod, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
handler.setFormatter(new XMLFormatter());
logger.addHandler(handler);

logger.logp(Level.INFO, "SampleClass", "sampleMethod",
        "TEST LOG!", "Param!");

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-10T08:08:20.067530Z</date>
//  <millis>1646899700067</millis>
//  <nanos>530000</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>SampleClass</class>
//  <method>sampleMethod</method>
//  <thread>1</thread>
//  <message>TEST LOG!</message>
//  <param>Param!</param>
//</record>

void logp (Level level, String sourceClass, String sourceMethod, String msg, Object[] params)

Log a message, specifying source class and method, with an array of object arguments.

Please see also : logp(Level level, String sourceClass, String sourceMethod, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
handler.setFormatter(new XMLFormatter());
logger.addHandler(handler);

logger.logp(Level.INFO, "SampleClass", "sampleMethod",
        "TEST LOG!", new String[]{"Param A", "Param B", "Param C"});

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-10T08:10:18.473365300Z</date>
//  <millis>1646899818473</millis>
//  <nanos>365300</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>SampleClass</class>
//  <method>sampleMethod</method>
//  <thread>1</thread>
//  <message>TEST LOG!</message>
//  <param>Param A</param>
//  <param>Param B</param>
//  <param>Param C</param>
//</record>

void logp (Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown)

Log a message, specifying source class and method, with associated Throwable information.

Please see also : logp(Level level, String sourceClass, String sourceMethod, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.logp(Level.SEVERE, "SampleClass", "sampleMethod",
        "TEST LOG!", new IllegalStateException("TEST EXCEPTION!"));

// Result
// ↓
//Mar 12, 2022 9:35:26 PM SampleClass sampleMethod
//SEVERE: TEST LOG!
//java.lang.IllegalStateException: TEST EXCEPTION!
//	at com.example.logging.LoggerTest$26.method(LoggerTest.java:1538)
//...

void logp (Level level, String sourceClass, String sourceMethod, Throwable thrown, Supplier<String> msgSupplier)

Log a lazily constructed message, specifying source class and method, with associated Throwable information.

Please see also : logp(Level level, String sourceClass, String sourceMethod, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.logp(Level.SEVERE, "SampleClass", "sampleMethod",
        new IllegalStateException("TEST EXCEPTION!"), () -> {
            return "TEST LOG!";
        });

// Result
// ↓
//Mar 12, 2022 9:38:00 PM SampleClass sampleMethod
//SEVERE: TEST LOG!
//java.lang.IllegalStateException: TEST EXCEPTION!
//	at com.example.logging.LoggerTest$27.method(LoggerTest.java:1574)
//...

void logp (Level level, String sourceClass, String sourceMethod, Supplier<String> msgSupplier)

Log a lazily constructed message, specifying source class and method, with no arguments.

Please see also : logp(Level level, String sourceClass, String sourceMethod, String msg)

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

logger.logp(Level.INFO, "SampleClass", "sampleMethod", () -> {
    return "TEST LOG!";
});

// Result
// ↓
//Mar 12, 2022 9:39:39 PM SampleClass sampleMethod
//INFO: TEST LOG!

void logrb (Level level, String sourceClass, String sourceMethod, String bundleName, String msg)

Deprecated. Use logrb(java.util.logging.Level, java.lang.String, java.lang.String, java.util.ResourceBundle, java.lang.String, java.lang.Object...) instead.

Deprecated.

void logrb (Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Object param1)

Deprecated. Use logrb(java.util.logging.Level, java.lang.String, java.lang.String, java.util.ResourceBundle, java.lang.String, java.lang.Object...) instead

Deprecated.

void logrb (Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Object[] params)

Deprecated. Use logrb(java.util.logging.Level, java.lang.String, java.lang.String, java.util.ResourceBundle, java.lang.String, java.lang.Object...) instead.

Deprecated.

void logrb (Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Throwable thrown)

Deprecated. Use logrb(java.util.logging.Level, java.lang.String, java.lang.String, java.util.ResourceBundle, java.lang.String, java.lang.Throwable) instead.

Deprecated.

void logrb (Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String msg, Object... params)

Log a message, specifying source class, method, and resource bundle, with an optional list of message parameters.

package com.example.logging;

import java.util.ListResourceBundle;

public class LoggerEnResourceBundle extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][]{
                {"SAMPLE", "XXX"}
        };
    }
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
handler.setFormatter(new XMLFormatter());
logger.addHandler(handler);

final var bundle = ResourceBundle.getBundle("com.example.logging.LoggerEnResourceBundle");

logger.logrb(Level.INFO, "SampleClass", "sampleMethod", bundle,
        "SAMPLE", "Param A", "Param B", "Param C");

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-13T05:44:46.767513400Z</date>
//  <millis>1647150286767</millis>
//  <nanos>513400</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>SampleClass</class>
//  <method>sampleMethod</method>
//  <thread>1</thread>
//  <message>XXX</message>
//  <key>SAMPLE</key>
//  <catalog>com.example.logging.LoggerEnResourceBundle</catalog>
//  <param>Param A</param>
//  <param>Param B</param>
//  <param>Param C</param>
//</record>

void logrb (Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String msg, Throwable thrown)

Log a message, specifying source class, method, and resource bundle, with associated Throwable information.

package com.example.logging;

import java.util.ListResourceBundle;

public class LoggerEnResourceBundle extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][]{
                {"SAMPLE", "XXX"}
        };
    }
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

final var bundle = ResourceBundle.getBundle("com.example.logging.LoggerEnResourceBundle");

logger.logrb(Level.SEVERE, "SampleClass", "sampleMethod", bundle,
        "SAMPLE", new IllegalStateException("TEST EXCEPTION!"));

// Result
// ↓
//Mar 12, 2022 9:46:50 PM SampleClass sampleMethod
//SEVERE: XXX
//java.lang.IllegalStateException: TEST EXCEPTION!
//	at com.example.logging.LoggerEnTest$4.method(LoggerEnTest.java:800)
//...

void logrb (Level level, ResourceBundle bundle, String msg, Object... params)

Log a message, specifying source class, method, and resource bundle, with an optional list of message parameters.

package com.example.logging;

import java.util.ListResourceBundle;

public class LoggerEnResourceBundle extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][]{
                {"SAMPLE", "XXX"}
        };
    }
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
handler.setFormatter(new XMLFormatter());
logger.addHandler(handler);

final var bundle = ResourceBundle.getBundle("com.example.logging.LoggerEnResourceBundle");

logger.logrb(Level.INFO, bundle, "SAMPLE", "Param A", "Param B", "Param C");

// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-13T05:52:13.311666100Z</date>
//  <millis>1647150733311</millis>
//  <nanos>666100</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.LoggerEnTest$5</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>XXX</message>
//  <key>SAMPLE</key>
//  <catalog>com.example.logging.LoggerEnResourceBundle</catalog>
//  <param>Param A</param>
//  <param>Param B</param>
//  <param>Param C</param>
//</record>

void logrb (Level level, ResourceBundle bundle, String msg, Throwable thrown)

Log a message, specifying source class, method, and resource bundle, with associated Throwable information.

package com.example.logging;

import java.util.ListResourceBundle;

public class LoggerEnResourceBundle extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][]{
                {"SAMPLE", "XXX"}
        };
    }
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

final var bundle = ResourceBundle.getBundle("com.example.logging.LoggerEnResourceBundle");

logger.logrb(Level.SEVERE, bundle, "SAMPLE", new IllegalStateException("TEST EXCEPTION!"));

// Result
// ↓
//Mar 12, 2022 9:53:46 PM com.example.logging.LoggerEnTest$6 method
//SEVERE: XXX
//java.lang.IllegalStateException: TEST EXCEPTION!
//	at com.example.logging.LoggerEnTest$6.method(LoggerEnTest.java:897)
//...

void removeHandler (Handler handler)

Remove a log Handler.

Please see getHandlers().

void setFilter (Filter newFilter)

Set a filter to control output on this Logger.

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

logger.addHandler(new ConsoleHandler());

System.out.println(logger.getFilter()); // null

logger.info("abcde");
logger.info("ABCDE");

// Result
// ↓
//Mar 12, 2022 9:55:25 PM com.example.logging.LoggerTest$33 method
//INFO: abcde
//Mar 12, 2022 9:55:25 PM com.example.logging.LoggerTest$33 method
//INFO: ABCDE

final var filter = new Filter() {
    @Override
    public boolean isLoggable(LogRecord logRecord) {
        return logRecord.getMessage().matches("[a-z]*");
    }
};

logger.setFilter(filter);

System.out.println(logger.getFilter() == filter); // true

logger.info("abcde");
logger.info("ABCDE");

// Result
// ↓
//Mar 12, 2022 9:55:25 PM com.example.logging.LoggerTest$33 method
//INFO: abcde

void setLevel (Level newLevel)

Set the log level specifying which message levels will be logged by this logger.

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);

logger.setLevel(Level.ALL);
System.out.println(logger.getLevel()); // ALL

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 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//SEVERE: SEVERE LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//WARNING: WARNING LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//INFO: INFO LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//CONFIG: CONFIG LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//FINE: FINE LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//FINER: FINER LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//FINEST: FINEST LOG!

logger.setLevel(Level.INFO);
System.out.println(logger.getLevel()); // INFO

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 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//SEVERE: SEVERE LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//WARNING: WARNING LOG!
//Mar 12, 2022 10:01:33 PM com.example.logging.LoggerTest$34 method
//INFO: INFO LOG!

logger.setLevel(Level.OFF);
System.out.println(logger.getLevel()); // OFF

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>

void setParent (Logger parent)

Set the parent for this Logger.

The API spec says "It should not be called from application code." Therefore, the code example is omitted.

void setResourceBundle (ResourceBundle bundle)

Sets a resource bundle on this logger.

package com.example.logging;

import java.util.ListResourceBundle;

public class LoggerEnResourceBundle extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][]{
                {"SAMPLE", "XXX"}
        };
    }
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.addHandler(new ConsoleHandler());

System.out.println(logger.getResourceBundle()); // null

final var bundle = ResourceBundle.getBundle("com.example.logging.LoggerEnResourceBundle");
logger.setResourceBundle(bundle);

System.out.println(logger.getResourceBundle() == bundle); // true

logger.info("SAMPLE");

// Result
// ↓
//Mar 12, 2022 10:06:05 PM com.example.logging.LoggerEnTest$7 method
//INFO: XXX

void setUseParentHandlers (boolean useParentHandlers)

Specify whether or not this logger should send its output to its parent Logger.

final var logger = Logger.getLogger("com.example.logging");
System.out.println(logger.getUseParentHandlers()); // true

logger.addHandler(new ConsoleHandler());

final var parent = Logger.getLogger("com.example");
parent.setUseParentHandlers(false);
System.out.println(parent.getUseParentHandlers()); // false

final var parentHandler = new ConsoleHandler();
parentHandler.setFormatter(new XMLFormatter());
parent.addHandler(parentHandler);

System.out.println(logger.getParent() == parent); // true

logger.info("TEST LOG : 1");

// Result (parent and child)
// ↓
//Mar 12, 2022 10:07:36 PM com.example.logging.LoggerTest$36 method
//INFO: TEST LOG : 1
//
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
//  <date>2022-03-13T06:07:36.234296300Z</date>
//  <millis>1647151656234</millis>
//  <nanos>296300</nanos>
//  <sequence>1</sequence>
//  <logger>com.example.logging</logger>
//  <level>INFO</level>
//  <class>com.example.logging.LoggerTest$36</class>
//  <method>method</method>
//  <thread>1</thread>
//  <message>TEST LOG : 1</message>
//</record>

logger.setUseParentHandlers(false);
System.out.println(logger.getUseParentHandlers()); // false

logger.info("TEST LOG : 2");

// Result (child only)
// ↓
//Mar 12, 2022 10:07:36 PM com.example.logging.LoggerTest$36 method
//INFO: TEST LOG : 2

void severe (String msg)

Log a SEVERE message.

Please see info(String msg).

void severe (Supplier<String> msgSupplier)

Log a SEVERE message, which is only to be constructed if the logging level is such that the message will actually be logged.

Please see info(Supplier<String> msgSupplier).

void throwing (String sourceClass, String sourceMethod, Throwable thrown)

Log throwing an exception.

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.throwing("SampleClass", "sampleMethod",
        new IllegalStateException("TEST EXCEPTION!"));

// Result
// ↓
//Mar 12, 2022 10:11:47 PM SampleClass sampleMethod
//FINER: THROW
//java.lang.IllegalStateException: TEST EXCEPTION!
//	at com.example.logging.LoggerTest$37.method(LoggerTest.java:2143)
//...

void warning (String msg)

Log a WARNING message.

Please see info(String msg).

void warning (Supplier<String> msgSupplier)

Log a WARNING message, which is only to be constructed if the logging level is such that the message will actually be logged.

Please see info(Supplier<String> msgSupplier).


Related posts

To top of page