- Direct Known Subclasses:
MemoryHandler
,StreamHandler
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.
- Since:
- 1.4
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract void
close()
Close theHandler
and free all associated resources.abstract void
flush()
Flush any buffered output.Return the character encoding for thisHandler
.Retrieves the ErrorManager for this Handler.Get the currentFilter
for thisHandler
.Return theFormatter
for thisHandler
.getLevel()
Get the log level specifying which messages will be logged by thisHandler
.boolean
isLoggable(LogRecord record)
Check if thisHandler
would actually log a givenLogRecord
.abstract void
Publish aLogRecord
.protected void
reportError(String msg, Exception ex, int code)
Protected convenience method to report an error to this Handler's ErrorManager.void
setEncoding(String encoding)
Set the character encoding used by thisHandler
.void
Define an ErrorManager for this Handler.void
Set aFilter
to control output on thisHandler
.void
setFormatter(Formatter newFormatter)
Set aFormatter
.void
Set the log level specifying which message levels will be logged by thisHandler
.
-
Constructor Details
-
Handler
protected Handler()Default constructor. The resultingHandler
has a log level ofLevel.ALL
, noFormatter
, and noFilter
. A defaultErrorManager
instance is installed as theErrorManager
.
-
-
Method Details
-
publish
Publish aLogRecord
.The logging request was made initially to a
Logger
object, which initialized theLogRecord
and forwarded it here.The
Handler
is responsible for formatting the message, when and if necessary. The formatting should include localization.- Parameters:
record
- description of the log event. A null record is silently ignored and is not published
-
flush
public abstract void flush()Flush any buffered output. -
close
Close theHandler
and free all associated resources.The close method will perform a
flush
and then close theHandler
. After close has been called thisHandler
should no longer be used. Method calls may either be silently ignored or may throw runtime exceptions.- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
setFormatter
Set aFormatter
. ThisFormatter
will be used to formatLogRecords
for thisHandler
.Some
Handlers
may not useFormatters
, in which case theFormatter
will be remembered, but not used.- Parameters:
newFormatter
- theFormatter
to use (may not be null)- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
getFormatter
Return theFormatter
for thisHandler
.- Returns:
- the
Formatter
(may be null).
-
setEncoding
Set the character encoding used by thisHandler
.The encoding should be set before any
LogRecords
are written to theHandler
.- Parameters:
encoding
- The name of a supported character encoding. May be null, to indicate the default platform encoding.- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.UnsupportedEncodingException
- if the named encoding is not supported.
-
getEncoding
Return the character encoding for thisHandler
.- Returns:
- The encoding name. May be null, which indicates the default encoding should be used.
-
setFilter
Set aFilter
to control output on thisHandler
.For each call of
publish
theHandler
will call thisFilter
(if it is non-null) to check if theLogRecord
should be published or discarded.- Parameters:
newFilter
- aFilter
object (may be null)- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
getFilter
Get the currentFilter
for thisHandler
.- Returns:
- a
Filter
object (may be null)
-
setErrorManager
Define an ErrorManager for this Handler.The ErrorManager's "error" method will be invoked if any errors occur while using this Handler.
- Parameters:
em
- the new ErrorManager- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
getErrorManager
Retrieves the ErrorManager for this Handler.- Returns:
- the ErrorManager for this Handler
- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
reportError
Protected convenience method to report an error to this Handler's ErrorManager. Note that this method retrieves and uses the ErrorManager without doing a security check. It can therefore be used in environments where the caller may be non-privileged.- Parameters:
msg
- a descriptive string (may be null)ex
- an exception (may be null)code
- an error code defined in ErrorManager
-
setLevel
Set the log level specifying which message levels will be logged by thisHandler
. 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
.- Parameters:
newLevel
- the new value for the log level- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
getLevel
Get the log level specifying which messages will be logged by thisHandler
. Message levels lower than this level will be discarded.- Returns:
- the level of messages being logged.
-
isLoggable
Check if thisHandler
would actually log a givenLogRecord
.This method checks if the
LogRecord
has an appropriateLevel
and whether it satisfies anyFilter
. It also may make otherHandler
specific checks that might prevent a handler from logging theLogRecord
. It will return false if theLogRecord
is null.- Parameters:
record
- aLogRecord
(may be null).- Returns:
- true if the
LogRecord
would be logged.
-