Re: informat IDE survey
On 3/20/2011 6:06 PM, Daniele Futtorovic wrote:
Yes, this second one is the one I've been thinking about in the
meantime. Extremely neat and powerful.
Here's what I came up with. Note that after checking the docs, I
noticed that MemoryHandler will self-push if its pushLevel is met or
exceeded. So if that's all the functionality you need you have it in
one class already.
The code below is completely untested.
package test;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.MemoryHandler;
/**
*
* @author Brenden
*/
public class TriggerHandler extends java.util.logging.Handler {
public TriggerHandler() {
setLevel( Level.SEVERE );
String slevel = LogManager.getLogManager().getProperty(
"logging.config.memorydump.trigger.level");
try {
Level level = Level.parse(slevel);
setLevel( level );
} catch( IllegalArgumentException ex ) {
// bail, log this?
}
}
@Override
public void publish(LogRecord record) {
if( record.getLevel().intValue() >= getLevel().intValue() ) {
String logger = LogManager.getLogManager().getProperty(
"logging.config.memorydump.logger");
if( logger == null ) {
logger = ""; // root logger
}
Handler[] handlers = Logger.getLogger(logger).getHandlers();
for( Handler h : handlers ) {
if( h instanceof MemoryHandler ) {
MemoryHandler mh = (MemoryHandler)h;
mh.push();
mh.flush();
}
}
}
// Assume parent handlers will be invoked.
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
}