Skip to content

Instantly share code, notes, and snippets.

@hkarakose
Last active August 9, 2017 16:31
Show Gist options
  • Save hkarakose/9c52f14f90a641a9a128063cb2e70775 to your computer and use it in GitHub Desktop.
Save hkarakose/9c52f14f90a641a9a128063cb2e70775 to your computer and use it in GitHub Desktop.
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