Java : StreamHandler (logging) with Examples

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


Summary

Stream based logging Handler.

Class diagram

final StreamHandler 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:25:49 PM com.example.logging.StreamHandlerTest$1 method
//INFO: TEST LOG!

Constructors

StreamHandler ()

Create a StreamHandler, with no current output stream.

final var outputStream = new ByteArrayOutputStream();

class SampleStreamHandler extends StreamHandler {
    SampleStreamHandler() {
        super();
        setOutputStream(outputStream);
    }
}

final var handler = new SampleStreamHandler();

System.out.println(handler.getLevel()); // INFO
System.out.println(handler.getFormatter()); // java.util.logging.SimpleFormatter@76d7e04

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

logger.info("TEST LOG!");
handler.flush();

System.out.print(outputStream);

// Result
// ↓
//Mar 14, 2022 1:47:29 AM com.example.logging.StreamHandlerTest$1 method
//INFO: TEST LOG!

StreamHandler (OutputStream out, Formatter formatter)

Create a StreamHandler with a given Formatter and output stream.

final var outputStream = new ByteArrayOutputStream();

final var handler = new StreamHandler(outputStream, new SimpleFormatter());

System.out.println(handler.getLevel()); // INFO
System.out.println(handler.getFormatter()); // java.util.logging.SimpleFormatter@84e9fc3

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

logger.info("TEST LOG!");
handler.flush();

System.out.print(outputStream);

// Result
// ↓
//Mar 14, 2022 1:50:07 AM com.example.logging.StreamHandlerTest$2 method
//INFO: TEST LOG!

Methods

void close ()

Close the current output stream.

final var pattern = Path.of("R:", "java-work", "log.txt");

final StreamHandler handler = new FileHandler(pattern.toString());
handler.setFormatter(new SimpleFormatter());

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

logger.info("BEFORE CLOSE LOG!");
System.out.println(Files.readString(pattern));

// Result
// ↓
//Mar 14, 2022 1:54:26 AM com.example.logging.StreamHandlerTest$3 method
//INFO: BEFORE CLOSE LOG!

handler.close();

logger.info("AFTER CLOSE LOG!");
System.out.println(Files.readString(pattern));

// Result (Not output "AFTER CLOSE LOG!")
// ↓
//Mar 14, 2022 1:54:26 AM com.example.logging.StreamHandlerTest$3 method
//INFO: BEFORE CLOSE LOG!

void flush ()

Flush any buffered messages.

final var outputStream = new ByteArrayOutputStream();
final var handler = new StreamHandler(outputStream, new SimpleFormatter());

final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
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 14, 2022 2:30:08 AM com.example.logging.StreamHandlerTest$4 method
//INFO: TEST LOG!

boolean isLoggable (LogRecord record)

Check if this Handler would actually log a given LogRecord.

final var handler = new StreamHandler(new ByteArrayOutputStream(), new SimpleFormatter());
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
final var handler = new StreamHandler(new ByteArrayOutputStream(), new SimpleFormatter());
final var logRecord = new LogRecord(Level.INFO, "TEST LOG!");

System.out.println(handler.isLoggable(logRecord)); // true

handler.close();

System.out.println(handler.isLoggable(logRecord)); // false

void publish (LogRecord record)

Format and publish a LogRecord.

final var outputStream = new ByteArrayOutputStream();
final var handler = new StreamHandler(outputStream, new SimpleFormatter()) {
    @Override
    public synchronized void publish(LogRecord logRecord) {
        logRecord.setMessage(logRecord.getMessage() + ":override publish!");

        super.publish(logRecord);
    }
};

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

logger.info("TEST LOG!");
handler.flush();

System.out.print(outputStream);

// Result
// ↓
//Mar 14, 2022 2:34:43 AM com.example.logging.StreamHandlerTest$5 method
//INFO: TEST LOG!:override publish!

void setEncoding (String encoding)

Set (or change) the character encoding used by this Handler.

System.out.println(Charset.defaultCharset()); // UTF-8

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

final var outputStream = new ByteArrayOutputStream();
final var handler = new StreamHandler(outputStream, new SimpleFormatter());

System.out.println(handler.getEncoding()); // null

logger.addHandler(handler);

logger.info("TEST LOG : 〇△□×");
handler.flush();

// An example of outputting to the console for UTF-8.
System.out.print(outputStream);

// Result
// ↓
//Mar 14, 2022 2:35:20 AM com.example.logging.StreamHandlerTest$6 method
//INFO: TEST LOG : 〇△□×
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);

final var outputStream = new ByteArrayOutputStream();
final var handler = new StreamHandler(outputStream, new SimpleFormatter());
logger.addHandler(handler);

handler.setEncoding("Shift_JIS");
System.out.println(handler.getEncoding()); // Shift_JIS

logger.info("TEST LOG : 〇△□×");
handler.flush();

// An example of outputting to the console for UTF-8.
System.out.print(outputStream);

// Result (Garbled characters)
// ↓
//Mar 14, 2022 2:38:29 AM com.example.logging.StreamHandlerTest$7 method
//INFO: TEST LOG : �Z�����~

protected void setOutputStream (OutputStream out)

Change the output stream.

Please see StreamHandler().

Methods declared in Handler

getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setErrorManager, setFilter, setFormatter, setLevel

Please see the link below.


Related posts

To top of page