# MemoryHandler

public class **MemoryHandler** extends [Handler](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html)  
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 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-name\>.level specifies the level for the Handler (defaults to Level.ALL).
- \<handler-name\>.filter specifies the name of a Filter class to use (defaults to no Filter).
- \<handler-name\>.size defines the buffer size (defaults to 1000).
- \<handler-name\>.push defines the pushLevel (defaults to level.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

<br />

### Public Constructor Summary

|---|---|
|   | [MemoryHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#MemoryHandler())() Create a MemoryHandler and configure it based on LogManager configuration properties. |
|   | [MemoryHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#MemoryHandler(java.util.logging.Handler,%20int,%20java.util.logging.Level))([Handler](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html) target, int size, [Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html) pushLevel) Create a MemoryHandler. |

### Public Method Summary

|---|---|
| void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#close())() Close the Handler and free all associated resources. |
| void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#flush())() Causes a flush on the target Handler. |
| [Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html) | [getPushLevel](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#getPushLevel())() Get the pushLevel. |
| boolean | [isLoggable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#isLoggable(java.util.logging.LogRecord))([LogRecord](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/LogRecord.html) record) Check if this Handler would actually log a given LogRecord into its internal buffer. |
| synchronized void | [publish](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#publish(java.util.logging.LogRecord))([LogRecord](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/LogRecord.html) record) Store a LogRecord in an internal buffer. |
| synchronized void | [push](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#push())() Push any buffered output to the target Handler. |
| synchronized void | [setPushLevel](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/MemoryHandler.html#setPushLevel(java.util.logging.Level))([Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html) newLevel) Set the pushLevel. |

### Inherited Method Summary

From class [java.util.logging.Handler](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#close())() Close the Handler and free all associated resources. |
| abstract void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#flush())() Flush any buffered output. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [getEncoding](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#getEncoding())() Return the character encoding for this Handler. |
| [ErrorManager](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/ErrorManager.html) | [getErrorManager](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#getErrorManager())() Retrieves the ErrorManager for this Handler. |
| [Filter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Filter.html) | [getFilter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#getFilter())() Get the current Filter for this Handler. |
| [Formatter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Formatter.html) | [getFormatter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#getFormatter())() Return the Formatter for this Handler. |
| [Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html) | [getLevel](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#getLevel())() Get the log level specifying which messages will be logged by this Handler. |
| boolean | [isLoggable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#isLoggable(java.util.logging.LogRecord))([LogRecord](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/LogRecord.html) record) Check if this Handler would actually log a given LogRecord. |
| abstract void | [publish](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#publish(java.util.logging.LogRecord))([LogRecord](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/LogRecord.html) record) Publish a LogRecord. |
| void | [reportError](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#reportError(java.lang.String,%20java.lang.Exception,%20int))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) msg, [Exception](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Exception.html) ex, int code) Protected convenience method to report an error to this Handler's ErrorManager. |
| synchronized void | [setEncoding](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#setEncoding(java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) encoding) Set the character encoding used by this Handler. |
| synchronized void | [setErrorManager](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#setErrorManager(java.util.logging.ErrorManager))([ErrorManager](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/ErrorManager.html) em) Define an ErrorManager for this Handler. |
| synchronized void | [setFilter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#setFilter(java.util.logging.Filter))([Filter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Filter.html) newFilter) Set a Filter to control output on this Handler. |
| synchronized void | [setFormatter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#setFormatter(java.util.logging.Formatter))([Formatter](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Formatter.html) newFormatter) Set a Formatter. |
| synchronized void | [setLevel](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html#setLevel(java.util.logging.Level))([Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html) newLevel) Set the log level specifying which message levels will be logged by this Handler. |

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Public Constructors

#### public
**MemoryHandler**
()

Create a MemoryHandler and configure it based on
LogManager configuration properties.

#### public
**MemoryHandler**
([Handler](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Handler.html) target, int size, [Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html) 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if `size is <= 0` |
|---|---|

## Public Methods

#### public void
**close**
()

Close the Handler and free all associated resources.
This will also close the target Handler.

##### Throws

| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | if a security manager exists and if the caller does not have LoggingPermission("control"). |
|---|---|

#### 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".

#### public [Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html)
**getPushLevel**
()

Get the pushLevel.

##### Returns

- the value of the pushLevel

#### public boolean
**isLoggable**
([LogRecord](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/LogRecord.html) 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.

<br />

##### Parameters

| record | a LogRecord |
|---|---|

##### Returns

- true if the LogRecord would be logged.

#### public synchronized void
**publish**
([LogRecord](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/LogRecord.html) 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 output
Handler.

##### Parameters

| record | description of the log event. A null record is silently ignored and is not published |
|---|---|

#### public synchronized void
**push**
()

Push any buffered output to the target Handler.


The buffer is then cleared.

#### public synchronized void
**setPushLevel**
([Level](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/logging/Level.html) newLevel)

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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | if a security manager exists and if the caller does not have LoggingPermission("control"). |
|---|---|