[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Logging Exceptions

Here is an example of logging exceptions that are thrown from methods that
exit via exceptions or are handled. This aspect uses a weak hash set to log
a throwable only the first time it is encountered:

public aspect RecordFirstExceptions {
   public pointcut scope() : within(com.xxx..*);

   public pointcut publicMethodExec() : 
       execution(public * *(..)) || execution(public new(..));

   after() throwing (Throwable t) : publicExec() && scope() {
       recordThrowable(t, thisJoinPointStaticPart,
thisEnclosingJoinPointStaticPart);
    }

    before(Throwable t) : handler(*) && args(t) && scope() {
        recordThrowable(t, thisJoinPointStaticPart,
thisEnclosingJoinPointStaticPart);
    }

    protected synchronized void recordThrowable(Throwable throwable,
StaticPart location, StaticPart enclosing) {
        // only record values once
        if (!throwablesRecorded.contains(throwable)) {
            throwablesRecorded.put(throwable);
            // log as desired
        }
    }

    private Set throwablesRecorded = new WeakHashSet();
}


-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Kees de Kooter
Sent: Wednesday, March 15, 2006 4:06 AM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] Logging Exceptions

Very nice solution Matthew!

Could you also give us an example of the after advice for RuntimeExceptions?

thanx,
Kees de Kooter

On 3/15/06, Matthew Webster <matthew_webster@xxxxxxxxxx> wrote:
>
> There have been a couple of questions on this subject. The most common
> approach is to use the point where an exception is caught:
>
> public aspect ErrorLogging {
>
>         private Logger logger = Logger.getAnonymousLogger();
>
>         before (Exception ex) : handler(Exception+) && args(ex) {
>         Signature sig =
> thisJoinPointStaticPart.getSignature();
>         logger.logp(Level.SEVERE,sig.getDeclaringType().getName(),
> sig.getName(), ex.toString());
>         }
> }
>
> This avoids getting a message for every method on the stack between where
> the exception is thrown and where it is caught. Sometimes the exception is
> not caught within your code and you would like to log it before control is
> returned to a caller. In this case using after throwing advice at the
> interface boundary is the best approach. This is usually the case for
> RuntimeExceptions.
>
> Matthew Webster
>  AOSD Project
>  Java Technology Centre, MP146
>  IBM Hursley Park, Winchester,  SO21 2JN, England
>  Telephone: +44 196 2816139 (external) 246139 (internal)
>  Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
> http://w3.hursley.ibm.com/~websterm/
>
> Please respond to aspectj-users@xxxxxxxxxxx
>
> Sent by:        aspectj-users-bounces@xxxxxxxxxxx
>
> To:        aspectj-users@xxxxxxxxxxx
> cc:
> Subject:        [aspectj-users] Logging Exceptions
>
>
>
> What is the best way to log Exceptions?
>
>  In code below, I show a simple program to test Logging Exceptions..
>
>  But in this example, will appear somethig like this in log>(Is this the
>  best way???? I think not.. but what is better?)
>  13/03/2006 15:48:17 Test coisa
>  SEVERE: java.lang.RuntimeException: bar
>  13/03/2006 15:48:17 Test foo
>  SEVERE: java.lang.RuntimeException: bar
>  13/03/2006 15:48:17 Test main
>  SEVERE: java.lang.RuntimeException: bar
>  Exception in thread "main" java.lang.Exception: bar
>     at Test.foo(Test.java:15)
>     at Test.main(Test.java:10)
>
>  Example:
>  I have this class:
>
>  public class Test {
>     public static void main(String s[]) throws Exception{
>           foo(new Test());
>     }
>
>     private static void foo(Test t) throws Exception{
>         throw new Exception("bar");
>     }
>  }
>
>  And I have this aspect:
>
>  import java.util.logging.*;
>  import org.aspectj.lang.*;
>
>  public privileged aspect TestAspect {
>     public static final Logger LOGGER =
>  Logger.getLogger(TestAspect.class.getName());
>
>     after() throwing (Exception ex): within(Test){
>         Signature sig =
> thisJoinPointStaticPart.getSignature();
>         LOGGER.logp(Level.SEVERE,
>  sig.getDeclaringType().getSimpleName(), sig.getName(), ex.toString());
>     }
>  }
>
>
>
>  _______________________________________________________
>  Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador
agora!
>  http://br.acesso.yahoo.com
>  _______________________________________________
>  aspectj-users mailing list
>  aspectj-users@xxxxxxxxxxx
>  https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
>
>
>
>
>
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users