Simplify Exception Logging with AspectJ

compared with
Current by Kees de Kooter
on Mar 17, 2006 09:06.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (13)

View Page History
h3. Introduction

{color:red}*ADVANCE WARNING: this is a work in progress and not a working solution yet!!*{color}

Some snippets from [http://www.developer.com/java/other/print.php/3109831]
{panel}

private Log log = LogFactory.getLog(this.getClass());
private Map loggedThrowables = new WeakHashMap();

public pointcut scope(): within(nl.boplicity..*);

before (Exception e): handler(Exception+) && args(e) after() throwing(Throwable t): scope() {

Signature signature = thisJoinPointStaticPart.getSignature(); logThrowable(t, thisJoinPointStaticPart,
thisEnclosingJoinPointStaticPart);
}

before (Throwable t): handler(Exception+) && args(t) && scope() {

// Create class part of log message, append 1 to line number logThrowable(t, thisJoinPointStaticPart,
thisEnclosingJoinPointStaticPart);
}

protected synchronized void logThrowable(Throwable t, StaticPart location,
// to be consistent with normal log4j results StaticPart enclosing) {
String source = signature.getDeclaringTypeName() + ":" +
(thisJoinPoint.getSourceLocation().getLine() + 1);

log.error(source + " - " + e.toString(), e); if (!loggedThrowables.containsKey(t)) {
loggedThrowables.put(t, null);

Signature signature = location.getSignature();

String source = signature.getDeclaringTypeName() + ":" +
(enclosing.getSourceLocation().getLine());

log.error("(a) " + source + " - " + t.toString(), t);
}
}
}
</category>
{code}

This is all working nicely in a tiny test app. Now for the real real thing: a webapp under tomcat.