O'Reilly logo

AspectJ Cookbook by Russ Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

21.2. Applying Aspect-Oriented Logging

Problem

You want to apply logging in a modular and application independent way.

Solution

Create an abstract Logging aspect that extends the generic TracingAspect from Recipe 21.1. Extend the TracingAspect with abstract pointcut definitions and advice for exception logging to complete the logging aspect, as shown in Example 21-3. The new exception logging abstract pointcut definitions allow specialized subaspects to capture areas of the target application where exception logging needs to take place.

Example 21-3. An abstract logging aspect that defines the generic logging logic to be specialized for your application

public abstract aspect LoggingAspect extends TracingAspect
{
   protected abstract pointcut exceptionsToBeLogged( );
   
   private pointcut filteredExceptionCapture( ) : 
      exceptionsToBeLogged( ) && 
      !pointsToBeExcluded( );
   
   before( ) : filteredExceptionCapture( )
   {
      logException(thisJoinPoint);
   }
   
   public abstract void logException(JoinPoint joinPoint);
}

Discussion

The LoggingAspect aspect inherits all of the behavior from the TracingAspect shown in Recipe 21.1 but adds some new capabilities specific to logging. The exceptionsToBeLogged() abstract pointcut is provided so specialized subaspects can specify the join points where exception information is to be logged. The logException(JoinPoint) abstract method allows subaspects to implement the exact behavior that will occur when exceptions are logged.

Tip

The handler(TypePattern) pointcut is the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required