Class MemoryHandler
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:
-
An incoming
LogRecordhas a type that is greater than a pre-defined level, thepushLevel. -
An external class calls the
pushmethod explicitly. -
A subclass overrides the
logmethod and scans each incomingLogRecordand callspushif 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-name>.level
specifies the level for the
Handler(defaults toLevel.ALL). - <handler-name>.filter
specifies the name of a
Filterclass 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
Handlerclass. (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
-
Constructor Summary
ConstructorsConstructorDescriptionCreate aMemoryHandlerand configure it based onLogManagerconfiguration properties.MemoryHandler(Handler target, int size, Level pushLevel) Create aMemoryHandler. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close theHandlerand free all associated resources.voidflush()Causes a flush on the targetHandler.Get thepushLevel.booleanisLoggable(LogRecord record) Check if thisHandlerwould actually log a givenLogRecordinto its internal buffer.voidStore aLogRecordin an internal buffer.voidpush()Push any buffered output to the targetHandler.voidsetPushLevel(Level newLevel) Set thepushLevel.Methods declared in class Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevelModifier and TypeMethodDescriptionReturn the character encoding for thisHandler.Retrieves the ErrorManager for this Handler.Get the currentFilterfor thisHandler.Return theFormatterfor thisHandler.getLevel()Get the log level specifying which messages will be logged by thisHandler.protected voidreportError(String msg, Exception ex, int code) Protected convenience method to report an error to this Handler's ErrorManager.voidsetEncoding(String encoding) Set the character encoding used by thisHandler.voidDefine an ErrorManager for this Handler.voidSet aFilterto control output on thisHandler.voidsetFormatter(Formatter newFormatter) Set aFormatter.voidSet the log level specifying which message levels will be logged by thisHandler.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Constructor Details
-
MemoryHandler
public MemoryHandler()Create aMemoryHandlerand configure it based onLogManagerconfiguration properties. -
MemoryHandler
Create aMemoryHandler.The
MemoryHandleris configured based onLogManagerproperties (or their default values) except that the givenpushLevelargument 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 Details
-
publish
Store aLogRecordin an internal buffer.If there is a
Filter, itsisLoggablemethod 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 thepushLevelthenpushis 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
-
close
-
setPushLevel
Set thepushLevel. After aLogRecordis copied into our internal buffer, if its level is greater than or equal to thepushLevel, thenpushwill be called.- Parameters:
newLevel- the new value of thepushLevel
-
getPushLevel
-
isLoggable
Check if thisHandlerwould actually log a givenLogRecordinto its internal buffer.This method checks if the
LogRecordhas an appropriate level and whether it satisfies anyFilter. However it does not check whether theLogRecordwould result in a "push" of the buffer contents. It will return false if theLogRecordis null.- Overrides:
isLoggablein classHandler- Parameters:
record- aLogRecord(may be null).- Returns:
- true if the
LogRecordwould be logged.
-