public class MemoryHandler extends Handler
Handler
that buffers requests in a circular buffer in memory.
Normally this Handler
simply stores incoming LogRecords
into its memory buffer and discards earlier records. This buffering
is very cheap and avoids formatting costs. On certain trigger
conditions, the MemoryHandler
will push out its current buffer
contents to a target Handler
, which will typically publish
them to the outside world.
There are three main models for triggering a push of the buffer:
LogRecord
has a type that is greater than
a pre-defined level, the pushLevel
. push
method explicitly. log
method and scans each incoming
LogRecord
and calls push
if a record matches some
desired criteria.
Configuration:
By default each MemoryHandler
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.
If no default value is defined then a RuntimeException is thrown.
Handler
(defaults to Level.ALL
). Filter
class to use
(defaults to no Filter
). pushLevel
(defaults to level.SEVERE
). Handler
class.
(no default).
For example, the properties for MemoryHandler
would be:
For a custom handler, e.g. com.foo.MyHandler, the properties would be:
Constructor and Description |
---|
MemoryHandler()
Create a
MemoryHandler and configure it based on
LogManager configuration properties. |
MemoryHandler(Handler target,
int size,
Level pushLevel)
Create a
MemoryHandler . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the
Handler and free all associated resources. |
void |
flush()
Causes a flush on the target
Handler . |
Level |
getPushLevel()
Get the
pushLevel . |
boolean |
isLoggable(LogRecord record)
Check if this
Handler would actually log a given
LogRecord into its internal buffer. |
void |
publish(LogRecord record)
Store a
LogRecord in an internal buffer. |
void |
push()
Push any buffered output to the target
Handler . |
void |
setPushLevel(Level newLevel)
Set the
pushLevel . |
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
public MemoryHandler()
MemoryHandler
and configure it based on
LogManager
configuration properties.public MemoryHandler(Handler target, int size, Level pushLevel)
MemoryHandler
.
The MemoryHandler
is configured based on LogManager
properties (or their default values) except that the given pushLevel
argument and buffer size argument are used.
target
- the Handler to which to publish output.size
- the number of log records to buffer (must be greater than zero)pushLevel
- message level to push onIllegalArgumentException
- if size is <= 0
public void publish(LogRecord record)
LogRecord
in an internal buffer.
If there is a Filter
, its isLoggable
method is called to check if the given log record is loggable.
If not we return. Otherwise the given record is copied into
an internal circular buffer. Then the record's level property is
compared with the pushLevel
. If the given level is
greater than or equal to the pushLevel
then push
is called to write all buffered records to the target output
Handler
.
public void push()
Handler
.
The buffer is then cleared.
public void flush()
Handler
.
Note that the current contents of the MemoryHandler
buffer are not written out. That requires a "push".
public void close() throws SecurityException
Handler
and free all associated resources.
This will also close the target Handler
.close
in class Handler
SecurityException
- if a security manager exists and if
the caller does not have LoggingPermission("control")
.public void setPushLevel(Level newLevel) throws SecurityException
pushLevel
. After a LogRecord
is copied
into our internal buffer, if its level is greater than or equal to
the pushLevel
, then push
will be called.newLevel
- the new value of the pushLevel
SecurityException
- if a security manager exists and if
the caller does not have LoggingPermission("control")
.public Level getPushLevel()
pushLevel
.pushLevel
public boolean isLoggable(LogRecord record)
Handler
would actually log a given
LogRecord
into its internal buffer.
This method checks if the LogRecord
has an appropriate level and
whether it satisfies any Filter
. However it does not
check whether the LogRecord
would result in a "push" of the
buffer contents. It will return false if the LogRecord
is null.
isLoggable
in class Handler
record
- a LogRecord
LogRecord
would be logged. Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2016, Oracle and/or its affiliates. All rights reserved.
DRAFT 9-internal+0-2016-01-26-133437.ivan.openjdk9onspinwait