MemoryHandler (Java Platform SE 8 ) (original) (raw)
- 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 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 LogRecord has a type that is greater than a pre-defined level, the pushLevel.
An external class calls the push method explicitly.
A subclass overrides the log method and scans each incomingLogRecord and calls push if a record matches some desired criteria.
Configuration: By default each MemoryHandler is initialized using the followingLogManager configuration properties where 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..level specifies the level for the Handler (defaults to Level.ALL).
.filter specifies the name of a Filter class to use (defaults to no Filter).
.size defines the buffer size (defaults to 1000).
.push defines the pushLevel (defaults to level.SEVERE).
.target specifies the name of the target Handler class. (no default).
For example, the properties forMemoryHandler
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.4Constructor Summary
Constructors
Constructor Description MemoryHandler() Create a MemoryHandler and configure it based onLogManager configuration properties. MemoryHandler(Handler target, int size,Level pushLevel) Create a MemoryHandler. Method Summary
All Methods Instance Methods Concrete Methods
Modifier and Type Method 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 givenLogRecord 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. * ### Methods inherited from class java.util.logging.[Handler](../../../java/util/logging/Handler.html "class in java.util.logging") `[getEncoding](../../../java/util/logging/Handler.html#getEncoding--), [getErrorManager](../../../java/util/logging/Handler.html#getErrorManager--), [getFilter](../../../java/util/logging/Handler.html#getFilter--), [getFormatter](../../../java/util/logging/Handler.html#getFormatter--), [getLevel](../../../java/util/logging/Handler.html#getLevel--), [reportError](../../../java/util/logging/Handler.html#reportError-java.lang.String-java.lang.Exception-int-), [setEncoding](../../../java/util/logging/Handler.html#setEncoding-java.lang.String-), [setErrorManager](../../../java/util/logging/Handler.html#setErrorManager-java.util.logging.ErrorManager-), [setFilter](../../../java/util/logging/Handler.html#setFilter-java.util.logging.Filter-), [setFormatter](../../../java/util/logging/Handler.html#setFormatter-java.util.logging.Formatter-), [setLevel](../../../java/util/logging/Handler.html#setLevel-java.util.logging.Level-)` * ### Methods inherited from class java.lang.[Object](../../../java/lang/Object.html "class in java.lang") `[clone](../../../java/lang/Object.html#clone--), [equals](../../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../../java/lang/Object.html#finalize--), [getClass](../../../java/lang/Object.html#getClass--), [hashCode](../../../java/lang/Object.html#hashCode--), [notify](../../../java/lang/Object.html#notify--), [notifyAll](../../../java/lang/Object.html#notifyAll--), [toString](../../../java/lang/Object.html#toString--), [wait](../../../java/lang/Object.html#wait--), [wait](../../../java/lang/Object.html#wait-long-), [wait](../../../java/lang/Object.html#wait-long-int-)`
Constructor Detail
* #### MemoryHandler public MemoryHandler() Create a MemoryHandler and configure it based on LogManager configuration properties. * #### MemoryHandler public MemoryHandler([Handler](../../../java/util/logging/Handler.html "class in java.util.logging") target, int size, [Level](../../../java/util/logging/Level.html "class in java.util.logging") pushLevel) Create a 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. 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](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if `size is <= 0`
Method Detail
* #### publish public void publish([LogRecord](../../../java/util/logging/LogRecord.html "class in java.util.logging") record) Store a 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 outputHandler. Specified by: `[publish](../../../java/util/logging/Handler.html#publish-java.util.logging.LogRecord-)` in class `[Handler](../../../java/util/logging/Handler.html "class in java.util.logging")` Parameters: `record` \- description of the log event. A null record is silently ignored and is not published * #### push public void push() Push any buffered output to the target Handler . The buffer is then cleared. * #### flush public void flush() Causes a flush on the target Handler . Note that the current contents of the MemoryHandler buffer are **not** written out. That requires a "push". Specified by: `[flush](../../../java/util/logging/Handler.html#flush--)` in class `[Handler](../../../java/util/logging/Handler.html "class in java.util.logging")` * #### close public void close() throws [SecurityException](../../../java/lang/SecurityException.html "class in java.lang") Close the Handler and free all associated resources. This will also close the target Handler . Specified by: `[close](../../../java/util/logging/Handler.html#close--)` in class `[Handler](../../../java/util/logging/Handler.html "class in java.util.logging")` Throws: `[SecurityException](../../../java/lang/SecurityException.html "class in java.lang")` \- if a security manager exists and if the caller does not have LoggingPermission("control"). * #### setPushLevel public void setPushLevel([Level](../../../java/util/logging/Level.html "class in java.util.logging") newLevel) throws [SecurityException](../../../java/lang/SecurityException.html "class in java.lang") Set the 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. Parameters: `newLevel` \- the new value of the pushLevel Throws: `[SecurityException](../../../java/lang/SecurityException.html "class in java.lang")` \- if a security manager exists and if the caller does not have LoggingPermission("control"). * #### getPushLevel public [Level](../../../java/util/logging/Level.html "class in java.util.logging") getPushLevel() Returns: the value of the pushLevel * #### isLoggable public boolean isLoggable([LogRecord](../../../java/util/logging/LogRecord.html "class in java.util.logging") record) Check if this 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. Overrides: `[isLoggable](../../../java/util/logging/Handler.html#isLoggable-java.util.logging.LogRecord-)` in class `[Handler](../../../java/util/logging/Handler.html "class in java.util.logging")` Parameters: `record` \- a LogRecord Returns: true if the 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, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.