Friday, April 10, 2009

AspectJ for Java logging: Serviceability without the clutter - part 6

Just realized I was missing the join point for constructors in the code. I also noticed the "getArgs" method in the "thisJoinPoint" object, which simplified the "methodError" point cut.

See the changes in red and the whole "Logging.aj" aspect as it stands today:

package my.project.aspects;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Aspect for Java logging throughout the code base.
*/
public aspect Logging pertypewithin(my..* && !Exception+) {

    private static Logger trace = Logger.getLogger("trace.my.project");

   /*
     * Pointcuts
     */
   
 
/**
* Initialization of all classes in the project.
*/
pointcut classInitializer() :
staticinitialization(my.project..*) &&
!within(Logging);

    /**

     * Execution body for all methods.
     */
    pointcut method():
(
execution
(my.project..*.new(..)) ||
        execution(* my
.project..*.*(..))
)

  &&
!within(my.project.D.*)
&&
!within(Logging);

    /**
     * Exception handling blocks inside all methods.
     */
    pointcut methodError():
        handler(handler(Throwable+) &&
!handler(InterruptedException+) &&

        within(my.project..*.*) &&
        !within(my.project.D.*) &&
        !within(Logging);

    /*
     * Join points
     */

    /**
     * After the initialization for all classes in this project.
     */
after(): classInitializer() {
String classname = thisJoinPointStaticPart.getSourceLocation().getWithinType().getName();
trace = Logger.getLogger("trace." + classname);
}

    /**
     * Entry trace statements for all methods.
     */
    before(): method() {
        if (trace.isLoggable(Level.FINER)) {
            trace.entering(
thisJoinPointStaticPart.getSourceLocation().getWithinType().getName(),

                    thisJoinPointStaticPart.getSignature().toString(),
                    thisJoinPoint.getArgs());
        }
    }

    /**
     * Exit trace statements for all methods.
     */
    after() returning (Object result): method() {
        if (trace.isLoggable(Level.FINER)) {
            trace.exiting(
thisJoinPointStaticPart
.getSourceLocation().getWithinType().getName(),

                    thisJoinPointStaticPart.getSignature().toString(),
                    result);
        }
    }

    /**
     * Exception handling blocks inside all methods.
     */
    before(): methodError() {
        if (trace.isLoggable(Level.FINER)) {
Object target = thisJoinPoint.getArgs()[0];
            trace.logp(Level.SEVERE,
thisJoinPointStaticPart.getSourceLocation().getWithinType().getName(),
                    thisJoinPointStaticPart.getSignature().toLongString(),
                    "Caught exception: " + target.getClass().toString(),
(Throwable)target);
        }
    }
 
}

No comments:

Post a Comment