FileDocCategorySizeDatePackage
MemoryHandler.javaAPI DocJava SE 5 API8380Fri Aug 26 14:57:28 BST 2005java.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 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. 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.

  • java.util.logging.MemoryHandler.level specifies the level for the Handler (defaults to Level.ALL).
  • java.util.logging.MemoryHandler.filter specifies the name of a Filter class to use (defaults to no Filter).
  • java.util.logging.MemoryHandler.size defines the buffer size (defaults to 1000).
  • java.util.logging.MemoryHandler.push defines the pushLevel (defaults to level.SEVERE).
  • java.util.logging.MemoryHandler.target specifies the name of the target Handler class. (no default).
version
1.24, 12/19/03
since
1.4

Fields Summary
private static final int
DEFAULT_SIZE
private Level
pushLevel
private int
size
private Handler
target
private LogRecord[]
buffer
int
start
int
count
Constructors Summary
public MemoryHandler()
Create a MemoryHandler and configure it based on LogManager configuration properties.

	sealed = false;
	configure();
	sealed = true;

	String name = "???";
	try {
            LogManager manager = LogManager.getLogManager();
	    name = manager.getProperty("java.util.logging.MemoryHandler.target");
	    Class clz = ClassLoader.getSystemClassLoader().loadClass(name);
	    target = (Handler) clz.newInstance();
	} catch (Exception ex) {
	    throw new RuntimeException("MemoryHandler can't load handler \"" + name + "\"" , ex);
	}
	init();
    
public MemoryHandler(Handler target, int size, Level 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.

param
target the Handler to which to publish output.
param
size the number of log records to buffer (must be greater than zero)
param
pushLevel message level to push on
throws
IllegalArgumentException is size is <= 0

	if (target == null || pushLevel == null) {
	    throw new NullPointerException();
	}
	if (size <= 0) {
	    throw new IllegalArgumentException();
	}
	sealed = false;
	configure();
	sealed = true;
	this.target = target;
	this.pushLevel = pushLevel;
	this.size = size;
	init();
    
Methods Summary
public voidclose()
Close the Handler and free all associated resources. This will also close the target Handler.

exception
SecurityException if a security manager exists and if the caller does not have LoggingPermission("control").

	target.close();
	setLevel(Level.OFF);
    
private voidconfigure()


    // Private method to configure a ConsoleHandler from LogManager
    // properties and/or default values as specified in the class
    // javadoc.
       
        LogManager manager = LogManager.getLogManager();
	String cname = getClass().getName();

	pushLevel = manager.getLevelProperty(cname +".push", Level.SEVERE);
	size = manager.getIntProperty(cname + ".size", DEFAULT_SIZE);
	if (size <= 0) {
	    size = DEFAULT_SIZE;
	}
	setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
	setFilter(manager.getFilterProperty(cname +".filter", null));
	setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
    
public voidflush()
Causes a flush on the target Handler.

Note that the current contents of the MemoryHandler buffer are not written out. That requires a "push".

	target.flush();
    
public synchronized java.util.logging.LevelgetPushLevel()
Get the pushLevel.

return
the value of the pushLevel

	return pushLevel;
    
private voidinit()

	buffer = new LogRecord[size];
	start = 0;
	count = 0;
    
public booleanisLoggable(java.util.logging.LogRecord 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.

param
record a LogRecord
return
true if the LogRecord would be logged.

	return super.isLoggable(record);
    
public synchronized voidpublish(java.util.logging.LogRecord 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.

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

	if (!isLoggable(record)) {
	    return;
	}
	int ix = (start+count)%buffer.length;
	buffer[ix] = record;
	if (count < buffer.length) {
	    count++;
	} else {
	    start++;
	}
	if (record.getLevel().intValue() >= pushLevel.intValue()) {
	    push();
	}
    
public synchronized voidpush()
Push any buffered output to the target Handler.

The buffer is then cleared.

	for (int i = 0; i < count; i++) {
	    int ix = (start+i)%buffer.length;
	    LogRecord record = buffer[ix];
	    target.publish(record);
	}
	// Empty the buffer.
	start = 0;
	count = 0;
    
public voidsetPushLevel(java.util.logging.Level 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.

param
newLevel the new value of the pushLevel
exception
SecurityException if a security manager exists and if the caller does not have LoggingPermission("control").

	if (newLevel == null) {
	    throw new NullPointerException();
	}
	LogManager manager = LogManager.getLogManager();
        checkAccess();
	pushLevel = newLevel;