Record the return value of the method with spring aop

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

Please, help!

+8
source share
4 answers

It turned out by simply writing the return value from the joinPoint.proceed() method.

+1
source

Using @AfterReturning with the returnValue parameter.

Then you could interact with the returned object. This is an example when I do this in everything, but get the methods in the 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); } } 
+13
source

In our case, most of the time we return spring modal entity classes, so we redefined the toString method of all entity classes with the required minimum information and printed 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."); } } 
+5
source

Spent a lot of time on this, so put it 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)); } } 
0
source

Source: https://habr.com/ru/post/1499173/


All Articles