APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.logging

public Class StreamHandler

extends Handler
Class Inheritance
Known Direct Subclasses
java.util.logging.ConsoleHandler, java.util.logging.FileHandler, java.util.logging.SocketHandler
Imports
java.io.*, java.util.Objects

Stream based logging Handler.

This is primarily intended as a base class or support class to be used in implementing other logging Handlers.

LogRecords are published to a given java.io.OutputStream.

Configuration: By default each StreamHandler is initialized using the following LogManager configuration properties where <handler-name> refers to the fully-qualified class name of the handler. If properties are not defined (or have invalid values) then the specified default values are used.

For example, the properties for StreamHandler would be:

For a custom handler, e.g. com.foo.MyHandler, the properties would be:

Since
1.4

Field Summary

Modifier and TypeField and Description
private boolean
private OutputStream
private volatile Writer

Constructor Summary

AccessConstructor and Description
public
StreamHandler()

Create a StreamHandler, with no current output stream.

public
StreamHandler(OutputStream
the target output stream
out
,
Formatter
Formatter to be used to format output
formatter
)

Create a StreamHandler with a given Formatter and output stream.

pack-priv
StreamHandler(Level defaultLevel, Formatter defaultFormatter, Formatter specifiedFormatter)

Method Summary

Modifier and TypeMethod and Description
public synchronized void
close()

Implements abstract java.util.logging.Handler.close.

Close the current output stream.

public synchronized void
flush()

Implements abstract java.util.logging.Handler.flush.

Flush any buffered messages.

private void
public boolean

Returns:

true if the LogRecord would be logged.
isLoggable
(LogRecord
a LogRecord (may be null).
record
)

Overrides java.util.logging.Handler.isLoggable.

Check if this Handler would actually log a given LogRecord.

public void
publish(LogRecord
description of the log event. A null record is silently ignored and is not published
record
)

Implements abstract java.util.logging.Handler.publish.

Format and publish a LogRecord.

public synchronized void
setEncoding(String
The name of a supported character encoding. May be null, to indicate the default platform encoding.
encoding
)

Overrides java.util.logging.Handler.setEncoding.

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

protected synchronized void
setOutputStream(OutputStream
New output stream. May not be null.
out
)

Change the output stream.

pack-priv void
synchronousPostWriteHook()

Overridden by other handlers in this package to facilitate synchronous post-write behaviour.

Inherited from java.util.logging.Handler:
getEncodinggetErrorManagergetFiltergetFormattergetLevelreportErrorsetErrorManagersetFiltersetFormattersetLevel

Field Detail

doneHeaderback to summary
private boolean doneHeader
outputback to summary
private OutputStream output
writerback to summary
private volatile Writer writer

Constructor Detail

StreamHandlerback to summary
public StreamHandler()

Create a StreamHandler, with no current output stream.

StreamHandlerback to summary
public StreamHandler(OutputStream out, Formatter formatter)

Create a StreamHandler with a given Formatter and output stream.

Parameters
out:OutputStream

the target output stream

formatter:Formatter

Formatter to be used to format output

StreamHandlerback to summary
pack-priv StreamHandler(Level defaultLevel, Formatter defaultFormatter, Formatter specifiedFormatter)
See Also
Handler#Handler(Level, Formatter, Formatter)

Method Detail

closeback to summary
public synchronized void close()

Implements abstract java.util.logging.Handler.close.

Close the current output stream.

The Formatter's "tail" string is written to the stream before it is closed. In addition, if the Formatter's "head" string has not yet been written to the stream, it will be written before the "tail" string.

Annotations
@Override
flushback to summary
public synchronized void flush()

Implements abstract java.util.logging.Handler.flush.

Flush any buffered messages.

Annotations
@Override
flushAndCloseback to summary
private void flushAndClose()
isLoggableback to summary
public boolean isLoggable(LogRecord record)

Overrides java.util.logging.Handler.isLoggable.

Check if this Handler would actually log a given LogRecord.

This method checks if the LogRecord has an appropriate level and whether it satisfies any Filter. It will also return false if no output stream has been assigned yet or the LogRecord is null.

Parameters
record:LogRecord

a LogRecord (may be null).

Returns:boolean

true if the LogRecord would be logged.

Annotations
@Override
publishback to summary
public void publish(LogRecord record)

Implements abstract java.util.logging.Handler.publish.

Format and publish a LogRecord.

The StreamHandler first checks if there is an OutputStream and if the given LogRecord has at least the required log level. If not it silently returns. If so, it calls any associated Filter to check if the record should be published. If so, it calls its Formatter to format the record and then writes the result to the current output stream.

If this is the first LogRecord to be written to a given OutputStream, the Formatter's "head" string is written to the stream before the LogRecord is written.

Implementation Specification

This method avoids acquiring locks during LogRecord formatting, but this instance is synchronized when writing to the output stream. To avoid deadlock risk, subclasses must not hold locks while calling super.publish(). Specifically, subclasses must not define the overridden publish() method to be synchronized if they call super.publish().

Parameters
record:LogRecord

description of the log event. A null record is silently ignored and is not published

Annotations
@Override
setEncodingback to summary
public synchronized void setEncoding(String encoding) throws UnsupportedEncodingException

Overrides java.util.logging.Handler.setEncoding.

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

The encoding should be set before any LogRecords are written to the Handler.

Parameters
encoding:String

The name of a supported character encoding. May be null, to indicate the default platform encoding.

Annotations
@Override
Exceptions
UnsupportedEncodingException:
if the named encoding is not supported.
setOutputStreamback to summary
protected synchronized void setOutputStream(OutputStream out)

Change the output stream.

If there is a current output stream then the Formatter's tail string is written and the stream is flushed and closed. Then the output stream is replaced with the new output stream.

Parameters
out:OutputStream

New output stream. May not be null.

synchronousPostWriteHookback to summary
pack-priv void synchronousPostWriteHook()

Overridden by other handlers in this package to facilitate synchronous post-write behaviour. If other handlers need similar functionality, it might be feasible to make this method protected (see JDK-8349206), but please find a better name if you do ;).