- java.lang.Object
-
- java.util.logging.Handler
-
- java.util.logging.MemoryHandler
-
public class MemoryHandler extends Handler
Handler
that buffers requests in a circular buffer in memory.Normally this
Handler
simply stores incomingLogRecords
into its memory buffer and discards earlier records. This buffering is very cheap and avoids formatting costs. On certain trigger conditions, theMemoryHandler
will push out its current buffer contents to a targetHandler
, which will typically publish them to the outside world.There are three main models for triggering a push of the buffer:
-
An incoming
LogRecord
has a type that is greater than a pre-defined level, thepushLevel
. -
An external class calls the
push
method explicitly. -
A subclass overrides the
log
method and scans each incomingLogRecord
and callspush
if a record matches some desired criteria.
Configuration: By default each
MemoryHandler
is initialized using the followingLogManager
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-name>.level
specifies the level for the
Handler
(defaults toLevel.ALL
). - <handler-name>.filter
specifies the name of a
Filter
class to use (defaults to noFilter
). - <handler-name>.size defines the buffer size (defaults to 1000).
- <handler-name>.push
defines the
pushLevel
(defaults tolevel.SEVERE
). - <handler-name>.target
specifies the name of the target
Handler
class. (no default).
For example, the properties for
MemoryHandler
would be:- java.util.logging.MemoryHandler.level=INFO
- java.util.logging.MemoryHandler.formatter=java.util.logging.SimpleFormatter
For a custom handler, e.g. com.foo.MyHandler, the properties would be:
- com.foo.MyHandler.level=INFO
- com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter
- Since:
- 1.4
-
An incoming
-
-
Constructor Summary
Constructors Constructor Description MemoryHandler()
Create aMemoryHandler
and configure it based onLogManager
configuration properties.MemoryHandler(Handler target, int size, Level pushLevel)
Create aMemoryHandler
.
-
Method Summary
Modifier and Type Method Description void
close()
Close theHandler
and free all associated resources.void
flush()
Causes a flush on the targetHandler
.Level
getPushLevel()
Get thepushLevel
.boolean
isLoggable(LogRecord record)
Check if thisHandler
would actually log a givenLogRecord
into its internal buffer.void
publish(LogRecord record)
Store aLogRecord
in an internal buffer.void
push()
Push any buffered output to the targetHandler
.void
setPushLevel(Level newLevel)
Set thepushLevel
.-
Methods declared in class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
-
-
-
-
Constructor Detail
-
MemoryHandler
public MemoryHandler()
Create aMemoryHandler
and configure it based onLogManager
configuration properties.
-
MemoryHandler
public MemoryHandler(Handler target, int size, Level pushLevel)
Create aMemoryHandler
.The
MemoryHandler
is configured based onLogManager
properties (or their default values) except that the givenpushLevel
argument and buffer size argument are used.- Parameters:
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 on- Throws:
IllegalArgumentException
- ifsize is <= 0
-
-
Method Detail
-
publish
public void publish(LogRecord record)
Store aLogRecord
in an internal buffer.If there is a
Filter
, itsisLoggable
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 thepushLevel
. If the given level is greater than or equal to thepushLevel
thenpush
is called to write all buffered records to the target outputHandler
.
-
push
public void push()
Push any buffered output to the targetHandler
.The buffer is then cleared.
-
flush
public void flush()
Causes a flush on the targetHandler
.Note that the current contents of the
MemoryHandler
buffer are not written out. That requires a "push".
-
close
public void close() throws SecurityException
Close theHandler
and free all associated resources. This will also close the targetHandler
.- Specified by:
close
in classHandler
- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
setPushLevel
public void setPushLevel(Level newLevel) throws SecurityException
Set thepushLevel
. After aLogRecord
is copied into our internal buffer, if its level is greater than or equal to thepushLevel
, thenpush
will be called.- Parameters:
newLevel
- the new value of thepushLevel
- Throws:
SecurityException
- if a security manager exists and if the caller does not haveLoggingPermission("control")
.
-
getPushLevel
public Level getPushLevel()
Get thepushLevel
.- Returns:
- the value of the
pushLevel
-
isLoggable
public boolean isLoggable(LogRecord record)
Check if thisHandler
would actually log a givenLogRecord
into its internal buffer.This method checks if the
LogRecord
has an appropriate level and whether it satisfies anyFilter
. However it does not check whether theLogRecord
would result in a "push" of the buffer contents. It will return false if theLogRecord
is null.- Overrides:
isLoggable
in classHandler
- Parameters:
record
- aLogRecord
- Returns:
- true if the
LogRecord
would be logged.
-
-