Announcement Announcement Module
Collapse
No announcement yet.
logging with AOP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • logging with AOP

    Hi,

    I'm trying to use AOP to automatically print something like the following whenever a method is entered/exited:

    Code:
    <Entering method: [ foo.bar.VoicePolicyMock.check ] param-0 [ 22 ]>
    
    <Exiting method: [ foo.bar.VoicePolicyMock.check ] return-val [ false ]>

    I've almost got it working, but there are a couple of problems. The method which does the logging is MethodLogger.log(ProceedingJoinPoint call), and is configured in the spring configuration file like this:

    Code:
    <bean id="loggingAspect"  class="com.acecomm.patrol.policy.aop.MethodLogger" />
    
    <aop:config>
      <aop:aspect ref="loggingAspect">
        <aop:pointcut id="myCutLogging"  expression="execution(* com.acecomm.patrol.policy.*.*(..))"/>
        <aop:around pointcut-ref="myCutLogging" method="log"/>
      </aop:aspect>
    </aop:config>
    This works fine, but if I change the pointcut expression to:
    Code:
    expression="execution(* com.acecomm.patrol.policy..*.*(..))"
    Then at runtime I get an error because CGLIB is not on the classpath. I tried adding cglib-1.1_beta1.jar, but then got a NoClassDefFoundError for org.objectweb.asm.Type. I've tried combining various different versions of ASM and CGLIB, but I usually end up getting a java.lang.StackOverflowError.

    Is there some problem with using this pointcut expression, or have I just not found the right library versions?

    Thanks in advance,
    DM
    Last edited by domurtag; Mar 12th, 2007, 07:16 PM.

  • #2
    Can you try with using the CGLIB and ASM shipped with Spring (assuming that you are using Spring 2.0.3, those would be cglib 2.1_3 and asm 2.2.2)?

    -Ramnivas

    Comment


    • #3
      Thanks very much - those libraries work perfectly. I have one minor problem remaining, I would like the log message that is printed on entering a method to look like this:

      Code:
      <Entering method: [ VoicePolicyValidator.supports ] param-0 [ true ]>
      But currently it looks like this:

      Code:
      <Entering method: [ VoicePolicyValidator.execution(supports) ] param-0 [ true ]>
      I could remove the extraneous "execution()" myself, but I expect there's probably some way to get just the method name of the target of the ProceedingJoinPoint. I've appended the code that builds this String in case you want to have a look.

      Thanks Again,
      DM


      Code:
      public class MethodLogger {
          
          private static final Logger LOG = Logger.getLogger(MethodLogger.class);
          
          public Object log(ProceedingJoinPoint call) throws Throwable {
      
              int paramIndex = 0;
              StringBuilder sb = new StringBuilder();
              sb.append("Entering method: ");
      
              //
              // Add the class name, then '.', then the method name
              //
              String methodName = "[ " + call.getTarget().getClass().getSimpleName() + '.'
                      + call.toShortString() + " ]";
              sb.append(methodName);
                      
              //
              // Append the method arguments
              //
              for (Object arg : call.getArgs()) {
                  sb.append(" param-" + paramIndex++);
      
                  if (arg == null) {
                      sb.append(" [ NULL ]");
                  } else {
                      sb.append(" [ " + arg + " ]");
                  }
              }
      
              LOG.trace(sb.toString());
              return call.proceed();        
          }
      }

      Comment


      • #4
        You should be able to obtain the method name using call.getSignature().getName(). You may want to explore the JoinPoint and ProceedingJoinPoint API for more details.

        -Ramnivas

        Comment


        • #5
          Works perfectly - thanks again!

          Comment

          Working...
          X