Java : LogManager (logging) with Examples
LogManager (Java SE 17 & JDK 17) API Examples.
You will find code examples on most LogManager methods.
Summary
The default configuration is typically loaded from the properties file below.
- java-home/conf/logging.properties
An excerpt from OpenJDK 17.0.1
############################################################
# Global properties
############################################################
handlers= java.util.logging.ConsoleHandler
# Default global logging level.
.level= INFO
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# Limit the messages that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
If you want to use another file, set the "java.util.logging.config.file" system property.
Example : sample.properties
handlers=java.util.logging.ConsoleHandler
.level=ALL
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.XMLFormatter
Main.java
import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("java.util.logging.config.file")); // sample.properties
final var logger = Logger.getLogger("");
System.out.println(logger.getLevel()); // ALL
final var handler = new ConsoleHandler();
System.out.println(handler.getLevel()); // ALL
System.out.println(handler.getFormatter()); // java.util.logging.XMLFormatter@3b07d329
}
}
java -D"java.util.logging.config.file=sample.properties" Main
Fields
static final String LOGGING_MXBEAN_NAME
// java.util.logging:type=Logging
System.out.println(LogManager.LOGGING_MXBEAN_NAME);
Constructors
LogManager ()
I think it's rare to create a subclass of LogManager. Therefore, the code example is omitted.
Methods
LogManager addConfigurationListener (Runnable listener)
final var logManager = LogManager.getLogManager();
final Runnable listener = () -> {
System.out.println("Listener OK!");
};
logManager.addConfigurationListener(listener);
logManager.updateConfiguration(null);
// Result
// ↓
//Listener OK!
logManager.removeConfigurationListener(listener);
logManager.updateConfiguration(null);
// Result
// ↓
//<No output>
boolean addLogger (Logger logger)
This method is called from log services. In general, you don't need to call this method. Therefore, the code example is omitted.
void checkAccess ()
Deprecated.
Logger getLogger (String name)
final var logManager = LogManager.getLogManager();
final var ret1 = logManager.getLogger("com.example.logging");
System.out.println(ret1); // null
final var ret2 = Logger.getLogger("com.example.logging");
System.out.println(ret2.getName()); // com.example.logging
final var ret3 = logManager.getLogger("com.example.logging");
System.out.println(ret2 == ret3); // true
Enumeration<String> getLoggerNames ()
final var logManager = LogManager.getLogManager();
// The empty name (the root logger) and Logger.GLOBAL_LOGGER_NAME.
final var ret1 = logManager.getLoggerNames();
System.out.println(Collections.list(ret1)); // [, global]
final var loggerA = Logger.getLogger("com.example.logging");
System.out.println(loggerA.getName()); // com.example.logging
final var ret2 = logManager.getLoggerNames();
System.out.println(Collections.list(ret2)); // [, global, com.example.logging]
final var loggerB = Logger.getLogger("com.example");
System.out.println(loggerB.getName()); // com.example
final var ret3 = logManager.getLoggerNames();
System.out.println(Collections.list(ret3)); // [, global, com.example, com.example.logging]
static LoggingMXBean getLoggingMXBean ()
Deprecated.
static LogManager getLogManager ()
final var logManager = LogManager.getLogManager();
System.out.println(logManager); // java.util.logging.LogManager@79acc4ff
String getProperty (String name)
final var logManager = LogManager.getLogManager();
// Global properties
final var ret1 = logManager.getProperty("handlers");
System.out.println(ret1); // java.util.logging.ConsoleHandler
final var ret2 = logManager.getProperty(".level");
System.out.println(ret2); // INFO
// ConsoleHandler
final var ret3 = logManager.getProperty("java.util.logging.ConsoleHandler.level");
System.out.println(ret3); // INFO
final var ret4 = logManager.getProperty("java.util.logging.ConsoleHandler.formatter");
System.out.println(ret4); // java.util.logging.SimpleFormatter
void readConfiguration ()
In general, you don't need to call this method. Therefore, the code example is omitted.
Please see API Note.
void readConfiguration (InputStream ins)
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.LogManager;
public class LoggerConfig {
public LoggerConfig() throws IOException {
final var properties = """
.level = ALL
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter
""";
try (final var ins = new ByteArrayInputStream(properties.getBytes())) {
final var logManager = LogManager.getLogManager();
logManager.readConfiguration(ins);
}
}
}
import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
final var ret = System.getProperty("java.util.logging.config.class");
System.out.println(ret); // LoggerConfig
final var logger = Logger.getLogger("");
System.out.println(logger.getLevel()); // ALL
final var handler = new ConsoleHandler();
System.out.println(handler.getLevel()); // ALL
System.out.println(handler.getFormatter()); // java.util.logging.XMLFormatter@3b07d329
}
}
java -D"java.util.logging.config.class=LoggerConfig" Main
void removeConfigurationListener (Runnable listener)
Please see addConfigurationListener(Runnable listener).
void reset ()
final var logger = Logger.getLogger("");
System.out.println(logger.getLevel()); // INFO
logger.setLevel(Level.ALL);
System.out.println(logger.getLevel()); // ALL
final var logManager = LogManager.getLogManager();
logManager.reset();
System.out.println(logger.getLevel()); // INFO
void updateConfiguration (InputStream ins, Function<String,BiFunction<String,String,String>> mapper)
final var logManager = LogManager.getLogManager();
{
final var ret1 = logManager.getProperty(".level");
System.out.println(ret1); // INFO
final var ret2 = logManager.getProperty("java.util.logging.ConsoleHandler.level");
System.out.println(ret2); // INFO
final var ret3 = logManager.getProperty("java.util.logging.ConsoleHandler.formatter");
System.out.println(ret3); // java.util.logging.SimpleFormatter
}
final var properties = """
.level=ALL
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.XMLFormatter
""";
try (final var ins = new ByteArrayInputStream(properties.getBytes())) {
logManager.updateConfiguration(ins, null);
}
{
final var ret1 = logManager.getProperty(".level");
System.out.println(ret1); // ALL
final var ret2 = logManager.getProperty("java.util.logging.ConsoleHandler.level");
System.out.println(ret2); // ALL
final var ret3 = logManager.getProperty("java.util.logging.ConsoleHandler.formatter");
System.out.println(ret3); // java.util.logging.XMLFormatter
}
final var logManager = LogManager.getLogManager();
final var properties = """
.level=ALL
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.XMLFormatter
""";
try (final var ins = new ByteArrayInputStream(properties.getBytes())) {
logManager.updateConfiguration(ins, (k) -> (o, n) -> {
if (k.equals(".level")) {
System.out.println(o); // INFO
System.out.println(n); // ALL
return "OFF";
} else {
return n;
}
});
}
final var ret1 = logManager.getProperty(".level");
System.out.println(ret1); // OFF
final var ret2 = logManager.getProperty("java.util.logging.ConsoleHandler.level");
System.out.println(ret2); // ALL
final var ret3 = logManager.getProperty("java.util.logging.ConsoleHandler.formatter");
System.out.println(ret3); // java.util.logging.XMLFormatter
void updateConfiguration (Function<String,BiFunction<String,String,String>> mapper)
final var logManager = LogManager.getLogManager();
{
final var ret1 = logManager.getProperty(".level");
System.out.println(ret1); // INFO
final var ret2 = logManager.getProperty("java.util.logging.ConsoleHandler.level");
System.out.println(ret2); // INFO
final var ret3 = logManager.getProperty("java.util.logging.ConsoleHandler.formatter");
System.out.println(ret3); // java.util.logging.SimpleFormatter
}
logManager.updateConfiguration((k) -> (o, n) -> {
if (k.equals(".level")) {
return "ALL";
} else {
return n;
}
});
{
final var ret1 = logManager.getProperty(".level");
System.out.println(ret1); // ALL
final var ret2 = logManager.getProperty("java.util.logging.ConsoleHandler.level");
System.out.println(ret2); // INFO
final var ret3 = logManager.getProperty("java.util.logging.ConsoleHandler.formatter");
System.out.println(ret3); // java.util.logging.SimpleFormatter
}