Skip to main content

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


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


Back to the top