Class MemoryHandler
public class MemoryHandler extends Handler
Handler put the description of log events into a cycled memory
buffer.
Mostly this MemoryHandler just puts the given LogRecord into
the internal buffer and doesn't perform any formatting or any other process.
When the buffer is full, the earliest buffered records will be discarded.
Every MemoryHandler has a target handler, and push action can be
triggered so that all buffered records will be output to the target handler
and normally the latter will publish the records. After the push action, the
buffer will be cleared.
The push method can be called directly, but will also be called automatically
if a new LogRecord is added that has a level greater than or
equal to than the value defined for the property
java.util.logging.MemoryHandler.push.
MemoryHandler will read following LogManager properties for
initialization, if given properties are not defined or has invalid values,
default value will be used.
- java.util.logging.MemoryHandler.filter specifies the
Filterclass name, defaults to noFilter. - java.util.logging.MemoryHandler.level specifies the level for this
Handler, defaults toLevel.ALL. - java.util.logging.MemoryHandler.push specifies the push level, defaults to level.SEVERE.
- java.util.logging.MemoryHandler.size specifies the buffer size in number
of
LogRecord, defaults to 1000. - java.util.logging.MemoryHandler.target specifies the class of the target
Handler, no default value, which means this property must be specified either by property setting or by constructor.
-
Constructor Summary
Constructors Constructor Description MemoryHandler()Default constructor, construct and init aMemoryHandlerusingLogManagerproperties or default values.MemoryHandler(Handler target, int size, Level pushLevel)Construct and init aMemoryHandlerusing given target, size and push level, other properties usingLogManagerproperties or default values. -
Method Summary
Modifier and Type Method Description voidclose()Close this handler and target handler, free all associated resources.voidflush()Call target handler to flush any buffered output.LevelgetPushLevel()Return the push level.booleanisLoggable(LogRecord record)Check if givenLogRecordwould be put into thisMemoryHandler's internal buffer.voidpublish(LogRecord record)Put a givenLogRecordinto internal buffer.voidpush()Triggers a push action to output all buffered records to the target handler, and the target handler will publish them.voidsetPushLevel(Level newLevel)Set the push level.Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
-
Constructor Details
-
MemoryHandler
public MemoryHandler()Default constructor, construct and init aMemoryHandlerusingLogManagerproperties or default values.- Throws:
RuntimeException- if property value are invalid and no default value could be used.
-
MemoryHandler
Construct and init aMemoryHandlerusing given target, size and push level, other properties usingLogManagerproperties or default values.- Parameters:
target- the givenHandlerto outputsize- the maximum number of bufferedLogRecord, greater than zeropushLevel- the push level- Throws:
IllegalArgumentException- ifsize <= 0RuntimeException- if property value are invalid and no default value could be used.
-
-
Method Details
-
close
public void close()Close this handler and target handler, free all associated resources. -
flush
public void flush()Call target handler to flush any buffered output. Note that this doesn't cause thisMemoryHandlerto push. -
publish
Put a givenLogRecordinto internal buffer. If given record is not loggable, just return. Otherwise it is stored in the buffer. Furthermore if the record's level is not less than the push level, the push action is triggered to output all the buffered records to the target handler, and the target handler will publish them. -
getPushLevel
Return the push level.- Returns:
- the push level
-
isLoggable
Check if givenLogRecordwould be put into thisMemoryHandler's internal buffer.The given
LogRecordis loggable if and only if it has appropriate level and it pass any associated filter's check.Note that the push level is not used for this check.
- Overrides:
isLoggablein classHandler- Parameters:
record- the givenLogRecord- Returns:
- the given
LogRecordif it should be logged,falseifLogRecordisnull.
-
push
public void push()Triggers a push action to output all buffered records to the target handler, and the target handler will publish them. Then the buffer is cleared. -
setPushLevel
Set the push level. The push level is used to check the push action triggering. When a newLogRecordis put into the internal buffer and its level is not less than the push level, the push action will be triggered. Note that set new push level won't trigger push action.- Parameters:
newLevel- the new level to set.
-