Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Generic Loggin of Method Parameters?

never mind i solved it.
for all the noobs like me, here is the code:

/**
*
*/
package at.systemone.examples.aop;
import org.apache.log4j.*;
import org.aspectj.lang.*;
import org.aspectj.lang.reflect.*;

public aspect LogMethod {
pointcut logMethod(): call (public * AOPLogging.*(..));
   before(): logMethod(){
Logger log = Logger.getLogger(assembleFullName(thisJoinPointStaticPart));
        Object[] paramValues= thisJoinPoint.getArgs();
String[] paramNames= ((CodeSignature)thisJoinPointStaticPart.getSignature()).getParameterNames();
        logParamValues(log, paramNames, paramValues);
   }
after() returning(Object o): logMethod(){ Logger log = Logger.getLogger(assembleFullName(thisJoinPointStaticPart));
       // Object returnValue= thisJoinPoint.getArgs()[0];
        if (log.isEnabledFor(Level.INFO)){
log.info("returns: '"+o+"'"); }
   }
private void logParamValues(Logger log, String[] paramNames, Object[] paramValues) { if (log.isEnabledFor(Level.INFO)) { // you can even gain perfomance by using the (otherwise unsexy) fast if clause
            String nv="(";
for (int i=0; i<paramValues.length; i++) { nv += paramNames[i]+" = '" + paramValues[i] +(i+1==paramValues.length?"')":"', ");
           }
            log.info(nv);
        }
   }
private String assembleFullName(JoinPoint.StaticPart joinPointStaticPart) {
       Signature sig = joinPointStaticPart.getSignature();
        String sign= sig.getDeclaringType().getName()+"."+sig.getName();
       return sign;
   }
}

Roland Kofler schrieb:

Thank you, it worked
but how can i get the return value?

i try to do something like this:

after(): logMethod(){
Logger log = Logger.getLogger(assembleFullName(thisJoinPointStaticPart));
        Object returnValue= ((MethodSignature)thisJoinPoint. ?? ;
        if (log.isEnabledFor(Level.INFO)){
            log.info("returns: '"+returnValue+"'");                     }
   }

thanks a lot
Roland


Stephan Kolp schrieb:

In your advices you can use the member thisJoinPoint. It is of type JoinPoint. JoinPoint has some methods that provide more information.

I think
    thisJoinPoint.getArgs()
will work.

See API-Doc (http://www.eclipse.org/aspectj/doc/released/api/index.html) for more Information.


Roland Kofler schrieb:

Hi all,
As a newbie I digged a half day for a generic solution that allows me to log any parameter I pass to any method in my package. But I cant find a way to get a "reflective" or "dynamic" list of parameters from a method's call

Can you give me advice ;-)?
Thank you a lot!

_______________________________________________
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




Back to the top