Skip to content

Instantly share code, notes, and snippets.

@hkarakose
Last active Aug 9, 2017
Embed
What would you like to do?
Aspect to intercept method executions and log method name, parameter values, duration and return values
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
@Aspect
public class MethodLogger {
private static final Logger LOGGER = Logger.getLogger(MethodLogger.class.getName());
@Around("execution(* com.example..*(..)) && !@annotation(DoNotLog) && !@annotation(Test)")
public Object around(ProceedingJoinPoint point) throws Throwable {
String methodMetadata = getMethodMetadata(point);
Method method = MethodSignature.class.cast(point.getSignature()).getMethod();
long start = System.currentTimeMillis();
try {
Object result = point.proceed();
long duration = System.currentTimeMillis() - start;
String resultValue = getResultValue(method, result);
String methodExecutionInfo = duration + "ms -->" + methodMetadata + "-->" + resultValue;
LOGGER.debug(methodExecutionInfo);
return result;
} catch (Exception e) {
throw new RuntimeException(methodMetadata, e);
}
}
private String getMethodMetadata(ProceedingJoinPoint point) {
Method method = MethodSignature.class.cast(point.getSignature()).getMethod();
Object[] args = point.getArgs();
Parameter[] parameters = method.getParameters();
StringBuilder formattedArgs = new StringBuilder();
for (int i = 0; i < args.length; i++) {
if (formattedArgs.length() != 0) {
formattedArgs.append(", ");
}
formattedArgs.append(parameters[i].getType().getSimpleName()).append(" ").append(parameters[i].getName())
.append(":").append(args[i]);
}
String className = method.getDeclaringClass().getSimpleName();
return className + "." + method.getName() + "(" + formattedArgs.toString() + ")";
}
private String getResultValue(Method method, Object result) {
String resultValue = "void";
if (!method.getReturnType().equals(Void.TYPE)) {
if (result == null) {
return null;
} else {
resultValue = result.getClass() + ":" + result;
}
}
return resultValue;
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface DoNotLog {
}
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<verbose>true</verbose>
<complianceLevel>1.8</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
</configuration>
</plugin>
</plugins>
</build>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment