Blammo Revisited
After yesterdays entry, I got two questions about the framework:
- We want to use an ATG logger; how do we do that?
- Is this convenient for debug logging?
Using other low level API's
Normally, if you need something else than the defaults, you are encouraged to inject this other implementation at runtime. You can however also set another default in your code that constructs the initial logger. Here is how you do it, assuming that you are working from the samples givin in my previous entry:
/*
* Using a JDK Logger.
*/
Logger logger = (Logger) BlammoLoggerFactory
.create(Logger.class, new BlammoJdkLog(java.util.logging.Logger.getLogger("Sample2")));
/*
* Or the shorthand:
*/
logger = (Logger) BlammoLoggerFactory.create(Logger.class, new BlammoJdkLogFactory());
/*
* Or do this:
*/
public voidSetBlammoLogFactory(BlammoLogFactory factory) {
logger = (Logger) BlammoLoggerFactory.create(Logger.class, factory);
}
Mixing low-level and Blammo logging
Blammo is most definitely not the ultimate tool for low level debug logging. In fact, you shoul not even consider Blammo for this. That should not be a problem at all, since you can mix and match Blammo with other low level logging mechanisms that you like:
package com.agilejava.blammo.samples;
import java.util.logging.Logger;
import com.agilejava.blammo.BlammoJdkLog;
import com.agilejava.blammo.BlammoLoggerFactory;
public class Sample2 {
private Logger logger = Logger.getLogger("Sample2");
private EventLogger eventLogger = (EventLogger) BlammoLoggerFactory
.create(EventLogger.class, new BlammoJdkLog(logger));
public int div(int a, int b) {
try {
logger.fine("About to do something nasty.");
return a / b;
} catch (ArithmeticException ae) {
if (b == 0) {
eventLogger.logDivisionByZero(a, b);
return 0;
} else {
eventLogger.logUnexpectedDivisionException(a, b, ae);
throw ae;
}
}
}
/**
* @param logger
* The logger to set.
*/
public void setLogger(EventLogger logger) {
this.eventLogger = logger;
}
/**
* @return Returns the logger.
*/
public EventLogger getLogger() {
return eventLogger;
}
/**
* @blammo.logger
*/
public interface EventLogger {
/**
* @blammo.level warning
* @blammo.message Attempt to divide {a} by {b}; returning 0 instead.
*/
void logDivisionByZero(int a, int b);
/**
* @blammo.level error
* @blammo.message Failed to divide {a} by {b}.
*/
void logUnexpectedDivisionException(int a, int b, ArithmeticException ae);
}
}
( Dec 08 2005, 02:54:25 PM CET )
Permalink
Comments [0]


