Package java.util.logging
Class StreamHandler
java.lang.Object
java.util.logging.Handler
java.util.logging.StreamHandler
- Direct Known Subclasses:
ConsoleHandler,FileHandler,SocketHandler
public class StreamHandler extends Handler
A
StreamHandler object writes log messages to an output stream, that
is, objects of the class OutputStream.
A StreamHandler object reads the following properties from the log
manager to initialize itself. A default value will be used if a property is
not found or has an invalid value.
- java.util.logging.StreamHandler.encoding specifies the encoding this handler will use to encode log messages. Default is the encoding used by the current platform.
- java.util.logging.StreamHandler.filter specifies the name of the filter
class to be associated with this handler. No
Filteris used by default. - java.util.logging.StreamHandler.formatter specifies the name of the
formatter class to be associated with this handler. Default is
java.util.logging.SimpleFormatter. - java.util.logging.StreamHandler.level specifies the logging level.
Defaults is
Level.INFO.
This class is not thread-safe.
-
Constructor Summary
Constructors Constructor Description StreamHandler()Constructs aStreamHandlerobject.StreamHandler(OutputStream os, Formatter formatter)Constructs aStreamHandlerobject with the supplied output stream and formatter. -
Method Summary
Modifier and Type Method Description voidclose()Closes this handler.voidflush()Flushes any buffered output.booleanisLoggable(LogRecord record)Determines whether the supplied log record needs to be logged.voidpublish(LogRecord record)Accepts a logging request.voidsetEncoding(String charsetName)Sets the character encoding used by this handler.protected voidsetOutputStream(OutputStream os)Sets the output stream this handler writes to.Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setErrorManager, setFilter, setFormatter, setLevel
-
Constructor Details
-
StreamHandler
public StreamHandler()Constructs aStreamHandlerobject. The new stream handler does not have an associated output stream. -
StreamHandler
Constructs aStreamHandlerobject with the supplied output stream and formatter.- Parameters:
os- the output stream this handler writes to.formatter- the formatter this handler uses to format the output.- Throws:
NullPointerException- ifosorformatterisnull.
-
-
Method Details
-
setOutputStream
Sets the output stream this handler writes to. If there's an existing output stream, the tail string of the associated formatter will be written to it. Then it will be flushed, closed and replaced withos.- Parameters:
os- the new output stream.- Throws:
NullPointerException- ifosisnull.
-
setEncoding
Sets the character encoding used by this handler. Anullvalue indicates that the default encoding should be used.- Overrides:
setEncodingin classHandler- Throws:
UnsupportedEncodingException- ifcharsetNameis not supported.
-
close
public void close()Closes this handler. The tail string of the formatter associated with this handler is written out. A flush operation and a subsequent close operation is then performed upon the output stream. Client applications should not use a handler after closing it. -
flush
public void flush()Flushes any buffered output. -
publish
Accepts a logging request. The log record is formatted and written to the output stream if the following three conditions are met:- the supplied log record has at least the required logging level;
- the supplied log record passes the filter associated with this handler, if any;
- the output stream associated with this handler is not
null.
-
isLoggable
Determines whether the supplied log record needs to be logged. The logging levels are checked as well as the filter. The output stream of this handler is also checked. If it isnull, this method returnsfalse.Notice : Case of no output stream will return
false.- Overrides:
isLoggablein classHandler- Parameters:
record- the log record to be checked.- Returns:
trueifrecordneeds to be logged,falseotherwise.
-