StreamHandler
is a handler in Java’s logging API that writes log records to a given OutputStream
. It is useful when you want to log messages to:
- A custom stream like
ByteArrayOutputStream
- A
Socket
stream - Any generic
OutputStream
(e.g., file, memory, network)
However, it does not flush automatically, so you must call flush()
or close()
to make sure the logs are written.
Commonly Used Methods

Simple Example
import java.io.*; import java.util.logging.*; public class SimpleStreamLogger { public static void main(String[] args) { Logger logger = Logger.getLogger("SimpleLogger"); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { StreamHandler streamHandler = new StreamHandler(out, new SimpleFormatter()); logger.addHandler(streamHandler); logger.setUseParentHandlers(false); // prevent console output logger.setLevel(Level.ALL); streamHandler.setLevel(Level.ALL); logger.info("This is an INFO message"); logger.warning("This is a WARNING message"); streamHandler.flush(); // must flush! // Print log output from memory System.out.println("Log Output from Stream:"); System.out.println(out.toString()); } catch (IOException e) { e.printStackTrace(); } } }
Output:
Log Output from Stream:
Jul 24, 2025 10:00:00 AM SimpleLogger main
INFO: This is an INFO message
Jul 24, 2025 10:00:00 AM SimpleLogger main
WARNING: This is a WARNING message
Code language: CSS (css)
Problem Statement
LotusJavaPrince is building a microservice where logs need to be sent to a custom stream and forwarded to a remote system. Mahesh wants a memory-efficient way to collect logs before pushing them. They use StreamHandler
to buffer logs in memory (ByteArrayOutputStream
) and only send them after a batch is complete.
import java.io.*; import java.util.logging.*; public class BatchTransactionLogger { private static final Logger logger = Logger.getLogger("BatchTransactionLogger"); private static ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(); private static StreamHandler streamHandler; static { streamHandler = new StreamHandler(bufferStream, new SimpleFormatter()); streamHandler.setLevel(Level.ALL); logger.addHandler(streamHandler); logger.setLevel(Level.ALL); logger.setUseParentHandlers(false); } public static void logTransaction(String user, double amount, boolean success) { if (success) { logger.info(user + " made a transaction of $" + amount); } else { logger.warning("Suspicious transaction by " + user + ": $" + amount); } } public static void flushAndSendLogs() { streamHandler.flush(); // must flush before reading String logs = bufferStream.toString(); System.out.println("Sending logs to remote system..."); System.out.println(logs); bufferStream.reset(); // clear buffer after sending } public static void main(String[] args) { logTransaction("LotusJavaPrince", 5000, true); logTransaction("Mahesh", 25000, false); logTransaction("Mahesh", 300, true); flushAndSendLogs(); // simulate sending log batch } }
Output:
Sending logs to remote system...
Jul 24, 2025 10:10:00 AM BatchTransactionLogger main
INFO: LotusJavaPrince made a transaction of $5000.0
Jul 24, 2025 10:10:00 AM BatchTransactionLogger main
WARNING: Suspicious transaction by Mahesh: $25000.0
Jul 24, 2025 10:10:00 AM BatchTransactionLogger main
INFO: Mahesh made a transaction of $300.0
StreamHandler
is a perfect match when you need intermediate log storage before pushing logs to a remote system, encrypting them, or analyzing them in memory.