Java : ConsoleHandler (logging) with Examples
ConsoleHandler (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ConsoleHandler methods.
Summary
final var handler = new ConsoleHandler();
System.out.println(handler.getLevel()); // INFO
System.out.println(handler.getFormatter()); // java.util.logging.SimpleFormatter@6be968ce
System.out.println(handler.getFilter()); // null
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.info("TEST LOG!");
// Result
// ↓
//Jan 10, 2023 11:32:08 PM com.example.logging.ConsoleHandlerTest$1 method
//INFO: TEST LOG!
Constructors
ConsoleHandler ()
final var handler = new ConsoleHandler();
System.out.println(handler.getLevel()); // INFO
System.out.println(handler.getFormatter()); // java.util.logging.SimpleFormatter@71a09cc3
System.out.println(handler.getFilter()); // null
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.info("TEST LOG!");
// Result
// ↓
//Mar 15, 2022 9:13:56 PM com.example.logging.ConsoleHandlerTest$1 method
//INFO: TEST LOG!
Methods
void close ()
final var handler = new ConsoleHandler();
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.info("BEFORE CLOSE LOG!");
handler.close();
logger.info("AFTER CLOSE LOG!");
// Result
// ↓
//Mar 15, 2022 9:14:53 PM com.example.logging.ConsoleHandlerTest$2 method
//INFO: BEFORE CLOSE LOG!
//Mar 15, 2022 9:14:53 PM com.example.logging.ConsoleHandlerTest$2 method
//INFO: AFTER CLOSE LOG!
void publish (LogRecord record)
final var handler = new ConsoleHandler() {
@Override
public synchronized void publish(LogRecord record) {
record.setMessage(record.getMessage() + " : override publish!");
super.publish(record);
}
};
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.info("TEST LOG!");
// Result
// ↓
//Mar 15, 2022 9:15:43 PM com.example.logging.ConsoleHandlerTest$3 method
//INFO: TEST LOG! : override publish!
Methods declared in StreamHandler
flush, isLoggable, setEncoding, setOutputStream
Please see the link below.
Methods declared in Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setErrorManager, setFilter, setFormatter, setLevel
Please see the link below.