Java : Handler (logging) with Examples
Handler (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Handler methods.
Summary
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
final Handler handler = new ConsoleHandler();
logger.addHandler(handler);
logger.info("TEST LOG!");
// Result (Console)
// ↓
//Jan 07, 2023 10:54:22 PM com.example.logging.HandlerTest$1 method
//INFO: TEST LOG!
Constructors
Handler ()
class SampleHandler extends Handler {
@Override
public void publish(LogRecord record) {
// Implement ...
}
@Override
public void flush() {
// Implement ...
}
@Override
public void close() throws SecurityException {
// Implement ...
}
}
final Handler handler = new SampleHandler();
System.out.println(handler.getLevel()); // ALL
System.out.println(handler.getFilter()); // null
System.out.println(handler.getFormatter()); // null
Methods
abstract void close ()
final var pattern = Path.of("R:", "java-work", "log.txt");
System.out.println(pattern); // R:\java-work\log.txt
final Handler handler = new FileHandler(pattern.toString());
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.info("TEST LOG!");
logger.removeHandler(handler);
handler.close();
System.out.println(Files.readString(pattern));
// Result
// ↓
//<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//<!DOCTYPE log SYSTEM "logger.dtd">
//<log>
//<record>
// <date>2022-03-13T10:43:28.380133800Z</date>
// <millis>1647168208380</millis>
// <nanos>133800</nanos>
// <sequence>1</sequence>
// <logger>com.example.logging</logger>
// <level>INFO</level>
// <class>com.example.logging.HandlerTest$1</class>
// <method>method</method>
// <thread>1</thread>
// <message>TEST LOG!</message>
//</record>
//</log>
abstract void flush ()
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
final var outputStream = new ByteArrayOutputStream();
final Handler handler = new StreamHandler(outputStream, new SimpleFormatter());
logger.addHandler(handler);
logger.info("TEST LOG!");
System.out.println("-- before flush --");
System.out.println(outputStream);
handler.flush();
System.out.println("-- after flush --");
System.out.println(outputStream);
// Result
// ↓
//-- before flush --
//
//-- after flush --
//Mar 13, 2022 4:41:49 AM com.example.logging.HandlerTest$2 method
//INFO: TEST LOG!
String getEncoding ()
System.out.println(Charset.defaultCharset()); // UTF-8
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
final Handler handler = new ConsoleHandler();
System.out.println(handler.getEncoding()); // null
logger.addHandler(handler);
logger.info("TEST LOG : 〇△□×");
// An example of outputting to the console for UTF-8.
// Result
// ↓
//Mar 13, 2022 4:46:10 AM com.example.logging.HandlerTest$3 method
//INFO: TEST LOG : 〇△□×
handler.setEncoding("Shift_JIS");
System.out.println(handler.getEncoding()); // Shift_JIS
logger.info("TEST LOG : 〇△□×");
// Result (Garbled characters)
// ↓
//Mar 13, 2022 4:46:10 AM com.example.logging.HandlerTest$3 method
//INFO: TEST LOG : �Z�����~
ErrorManager getErrorManager ()
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
final Handler handler = new ConsoleHandler() {
@Override
public void publish(LogRecord record) {
// Intentionally reports an exception.
reportError("TEST ERROR!",
new IllegalStateException("TEST EXCEPTION!"),
ErrorManager.GENERIC_FAILURE);
}
};
logger.addHandler(handler);
class CustomErrorManager extends ErrorManager {
@Override
public synchronized void error(String msg, Exception ex, int code) {
System.err.println("CUSTOM ERROR! : " + msg + " : " + ex.getMessage());
}
}
System.out.println(handler.getErrorManager() instanceof CustomErrorManager); // false
logger.info("TEST LOG!");
// Result
// ↓
//java.util.logging.ErrorManager: 0: TEST ERROR!
//java.lang.IllegalStateException: TEST EXCEPTION!
// at com.example.logging.HandlerTest$6$1.publish(HandlerTest.java:354)
//...
handler.setErrorManager(new CustomErrorManager());
System.out.println(handler.getErrorManager() instanceof CustomErrorManager); // true
logger.info("TEST LOG!");
// Result
// ↓
//CUSTOM ERROR! : TEST ERROR! : TEST EXCEPTION!
Filter getFilter ()
final Handler handler = new ConsoleHandler();
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.addHandler(handler);
System.out.println(handler.getFilter()); // null
logger.info("abcd");
logger.info("EFG");
logger.info("xyz");
// Result
// ↓
//Mar 13, 2022 4:55:38 AM com.example.logging.HandlerTest$5 method
//INFO: abcd
//Mar 13, 2022 4:55:39 AM com.example.logging.HandlerTest$5 method
//INFO: EFG
//Mar 13, 2022 4:55:39 AM com.example.logging.HandlerTest$5 method
//INFO: xyz
final var filter = new Filter() {
@Override
public boolean isLoggable(LogRecord record) {
final var msg = record.getMessage();
return msg.equals(msg.toLowerCase());
}
};
handler.setFilter(filter);
System.out.println(filter == handler.getFilter()); // true
logger.info("abcd");
logger.info("EFG");
logger.info("xyz");
// Result
// ↓
//Mar 13, 2022 4:55:39 AM com.example.logging.HandlerTest$5 method
//INFO: abcd
//Mar 13, 2022 4:55:39 AM com.example.logging.HandlerTest$5 method
//INFO: xyz
Formatter getFormatter ()
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
// ConsoleHandler has SimpleFormatter by default.
final Handler handler = new ConsoleHandler();
logger.addHandler(handler);
// java.util.logging.SimpleFormatter@6a8658ff
System.out.println(handler.getFormatter());
logger.info("TEST LOG!");
// Result
// ↓
//Mar 13, 2022 5:00:26 AM com.example.logging.HandlerTest$6 method
//INFO: TEST LOG!
handler.setFormatter(new XMLFormatter());
// java.util.logging.XMLFormatter@491b9b8
System.out.println(handler.getFormatter());
logger.info("TEST LOG!");
// Result
// ↓
//<record>
// <date>2022-03-13T12:00:26.414943800Z</date>
// <millis>1647172826414</millis>
// <nanos>943800</nanos>
// <sequence>2</sequence>
// <logger>com.example.logging</logger>
// <level>INFO</level>
// <class>com.example.logging.HandlerTest$6</class>
// <method>method</method>
// <thread>1</thread>
// <message>TEST LOG!</message>
//</record>
Level getLevel ()
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
final Handler handler = new ConsoleHandler();
logger.addHandler(handler);
System.out.println(handler.getLevel()); // INFO
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
// Result
// ↓
//Mar 13, 2022 5:02:45 AM com.example.logging.HandlerTest$7 method
//WARNING: WARNING LOG!
//Mar 13, 2022 5:02:45 AM com.example.logging.HandlerTest$7 method
//INFO: INFO LOG!
handler.setLevel(Level.FINE);
System.out.println(handler.getLevel()); // FINE
logger.warning("WARNING LOG!");
logger.info("INFO LOG!");
logger.fine("FINE LOG!");
logger.finer("FINER LOG!");
// Result
// ↓
//Mar 13, 2022 5:02:45 AM com.example.logging.HandlerTest$7 method
//WARNING: WARNING LOG!
//Mar 13, 2022 5:02:45 AM com.example.logging.HandlerTest$7 method
//INFO: INFO LOG!
//Mar 13, 2022 5:02:45 AM com.example.logging.HandlerTest$7 method
//FINE: FINE LOG!
boolean isLoggable (LogRecord record)
final Handler handler = new ConsoleHandler();
System.out.println(handler.getLevel()); // INFO
final var ret1 = handler.isLoggable(new LogRecord(Level.WARNING, "WARNING!"));
System.out.println(ret1); // true
final var ret2 = handler.isLoggable(new LogRecord(Level.INFO, "INFO!"));
System.out.println(ret2); // true
final var ret3 = handler.isLoggable(new LogRecord(Level.FINE, "FINE!"));
System.out.println(ret3); // false
final var ret4 = handler.isLoggable(new LogRecord(Level.FINER, "FINER!"));
System.out.println(ret4); // false
abstract void publish (LogRecord record)
Please see getErrorManager().
protected void reportError (String msg, Exception ex, int code)
Please see getErrorManager().
void setEncoding (String encoding)
Please see getEncoding().
void setErrorManager (ErrorManager em)
Please see getErrorManager().
void setFilter (Filter newFilter)
Please see getFilter().
void setFormatter (Formatter newFormatter)
Please see getFormatter().
void setLevel (Level newLevel)
Please see getLevel().
Related posts
- API Examples