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!
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!
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);
}
}
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.");
}
}
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));
}
}