12

I have a method which returns an object. I would like to print that object's value in my log using spring AOP. How can I achieve that?

Please help!

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
prabu
  • 1,247
  • 7
  • 21
  • 33

3 Answers3

18

Using @AfterReturning with a returnValue param.

You could then interogate the object returned This is an example where I do it on everything but get methods in a repository

@AfterReturning(value = "@target(org.springframework.stereotype.Repository) && !execution(* get*(..))", returning = "returnValue")
public void loggingRepositoryMethods(JoinPoint joinPoint, Object returnValue) {
    String classMethod = this.getClassMethod(joinPoint);



     if(returnValue !=null)
     {
       //test type of object get properties (could use reflection)
       log it out
     }
     else
     {
         //do logging here probably passing in (joinPoint, classMethod);
     }
}
Shaun Hare
  • 3,771
  • 2
  • 24
  • 36
  • Hi Shaun , any way to accomplish this using ProceedingJoinPoint class? – prabu Aug 28 '13 at 12:58
  • I found it.. I just printed the object returned by proceed() method of ProceedingJoinPoint.. Its printing the returned value. Thanks for your reply.. :) – prabu Aug 28 '13 at 13:12
  • Got it working by just logging the `return` value from `joinPoint.proceed()` method. – prabu Aug 28 '13 at 13:54
6

In our case, majority of the time we return entity classes of spring modal, so we overridden the toString method of all entity classes with required minimal information and printed as below

@AfterReturning(pointcut = "within(@org.springframework.stereotype.Service *)", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
    logger.info(" ###### Returning for class : {} ; Method : {} ", joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName());
    if (result != null) {
        logger.info(" ###### with value : {}", result.toString());
    } else{
        logger.info(" ###### with null as return value.");
    }
}
Jajikanth pydimarla
  • 1,512
  • 13
  • 11
0

Spent quite sometime on this, so putting here...

package com.mycomp.poc.JcachePoc.config;

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


import lombok.extern.slf4j.Slf4j;

@Aspect
@Component
@Slf4j
public class LoggingAspectConfig {

    private static LoggingAspectConfig loggingAspectConfig;


    @Bean
    public LoggingAspectConfig createBean() {
        if(loggingAspectConfig==null)
        return new LoggingAspectConfig();
        else
            return loggingAspectConfig;
    }


    private LoggingAspectConfig () {

    }

    @Before("execution(* com.mycom.poc.JcachePoc.service*.*.*(..)) && @annotation(Log)")
    public void logBefore(JoinPoint joinPoint) {
        if(log.isDebugEnabled()) {
            Object[] args= joinPoint.getArgs();
            Map<String, String> typeValue= new HashMap<>();
            for(Object obj: args) {
                if(obj!=null) {
                    typeValue.put(obj.getClass().getName(), obj.toString());
                }
            }
            //log.debug("calling Method:"+joinPoint.getSignature().getDeclaringTypeName()+", "+joinPoint.getSignature().getName()+", Parameter:-> "+ typeValue);
        }
    }

    @AfterReturning(pointcut = "execution(* com.mycom.poc.JcachePoc.service*.*.*(..)) && @annotation(Log)", returning = "result")
    public void logAfter(JoinPoint joinPoint, Object result) {
        if (log.isDebugEnabled() && result!=null) {         
            log.debug("Method returned:" + 
                    joinPoint.getSignature().getName() + ", Result: " + result.getClass().getName()+" -->"+result);
        }
        //log.info(gson.toJson(result));
    }

}
Vikky
  • 1,123
  • 14
  • 16