Re: informat IDE survey
On 21/03/2011 03:31, markspace allegedly wrote:
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.
My company's code is tied to log4j. Since I already have my trusted
logging layer for that...
public static void log( Logger logger, Level level, Throwable t, String
format, Object... args ){
if( level.isGreaterOrEqual(logger.getEffectiveLevel()) ){
logger.log( level, String.format(format, args), t );
}
}
.... I took another approach. I installed a ThreadLocal circular buffer,
its contents instances of a subclass of log4j's LoggingEvent (to avoid
doing the String#format needlessly). Then, when the log method above is
called: if it doesn't pass the level, it goes into the buffer;
otherwise, if it's Level.ERROR, the contents of the buffer are logged;
fi; then the statement is logged; fi.
I'm going to have a look how it works out in practice, but first results
were pleasing. Considering the standard production mode where little
is logged under normal circumstances, I get the full monty when
an error occurs. Kinda of the best of two worlds: no unnecessary
clutter, and all the details I need when it matters. In development, the
level is on full, of course.
It's still a bit shaky to my liking, but as far as I can see the
pernicious idiom:
if( logger.isMyLevelEnabled() )
logger.myLevel( message )
prohibits anything more solid.
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 {
}
}