MemoryHandler
, StreamHandler
public abstract class Handler extends Object
Handler
object takes log messages from a Logger
and exports them. It might for example, write them to a console or write them to a file, or send them to a network logging service, or forward them to an OS log, or whatever. A Handler
can be disabled by doing a setLevel(Level.OFF)
and can be re-enabled by doing a setLevel
with an appropriate level.
Handler
classes typically use LogManager
properties to set default values for the Handler
's Filter
, Formatter
, and Level
. See the specific documentation for each concrete Handler
class.
Modifier | Constructor | Description |
---|---|---|
protected |
Default constructor. |
Modifier and Type | Method | Description |
---|---|---|
abstract void |
close() |
Close the Handler and free all associated resources. |
abstract void |
flush() |
Flush any buffered output. |
String |
getEncoding() |
Return the character encoding for this Handler . |
ErrorManager |
getErrorManager() |
Retrieves the ErrorManager for this Handler. |
Filter |
getFilter() |
Get the current Filter for this Handler . |
Formatter |
getFormatter() |
Return the Formatter for this Handler . |
Level |
getLevel() |
Get the log level specifying which messages will be logged by this Handler . |
boolean |
isLoggable |
Check if this Handler would actually log a given LogRecord . |
abstract void |
publish |
Publish a LogRecord . |
protected void |
reportError |
Protected convenience method to report an error to this Handler's ErrorManager. |
void |
setEncoding |
Set the character encoding used by this Handler . |
void |
setErrorManager |
Define an ErrorManager for this Handler. |
void |
setFilter |
Set a Filter to control output on this Handler . |
void |
setFormatter |
Set a Formatter . |
void |
setLevel |
Set the log level specifying which message levels will be logged by this Handler . |
protected Handler()
Handler
has a log level of Level.ALL
, no Formatter
, and no Filter
. A default ErrorManager
instance is installed as the ErrorManager
.public abstract void publish(LogRecord record)
LogRecord
. The logging request was made initially to a Logger
object, which initialized the LogRecord
and forwarded it here.
The Handler
is responsible for formatting the message, when and if necessary. The formatting should include localization.
record
- description of the log event. A null record is silently ignored and is not publishedpublic abstract void flush()
public abstract void close() throws SecurityException
Handler
and free all associated resources. The close method will perform a flush
and then close the Handler
. After close has been called this Handler
should no longer be used. Method calls may either be silently ignored or may throw runtime exceptions.
SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")
.public void setFormatter(Formatter newFormatter) throws SecurityException
Formatter
. This Formatter
will be used to format LogRecords
for this Handler
. Some Handlers
may not use Formatters
, in which case the Formatter
will be remembered, but not used.
newFormatter
- the Formatter
to use (may not be null)SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")
.public Formatter getFormatter()
Formatter
for this Handler
.Formatter
(may be null).public void setEncoding(String encoding) throws SecurityException, UnsupportedEncodingException
Handler
. The encoding should be set before any LogRecords
are written to the Handler
.
encoding
- The name of a supported character encoding. May be null, to indicate the default platform encoding.SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")
.UnsupportedEncodingException
- if the named encoding is not supported.public String getEncoding()
Handler
.public void setFilter(Filter newFilter) throws SecurityException
Filter
to control output on this Handler
. For each call of publish
the Handler
will call this Filter
(if it is non-null) to check if the LogRecord
should be published or discarded.
newFilter
- a Filter
object (may be null)SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")
.public Filter getFilter()
Filter
for this Handler
.Filter
object (may be null)public void setErrorManager(ErrorManager em)
The ErrorManager's "error" method will be invoked if any errors occur while using this Handler.
em
- the new ErrorManagerSecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")
.public ErrorManager getErrorManager()
SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")
.protected void reportError(String msg, Exception ex, int code)
msg
- a descriptive string (may be null)ex
- an exception (may be null)code
- an error code defined in ErrorManagerpublic void setLevel(Level newLevel) throws SecurityException
Handler
. Message levels lower than this value will be discarded. The intention is to allow developers to turn on voluminous logging, but to limit the messages that are sent to certain Handlers
.
newLevel
- the new value for the log levelSecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")
.public Level getLevel()
Handler
. Message levels lower than this level will be discarded.public boolean isLoggable(LogRecord record)
Handler
would actually log a given LogRecord
. This method checks if the LogRecord
has an appropriate Level
and whether it satisfies any Filter
. It also may make other Handler
specific checks that might prevent a handler from logging the LogRecord
. It will return false if the LogRecord
is null.
record
- a LogRecord
(may be null).LogRecord
would be logged.
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.logging/java/util/logging/Handler.html