Skip to content

Instantly share code, notes, and snippets.

@mcaserta
Created November 24, 2011 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcaserta/1391757 to your computer and use it in GitHub Desktop.
Save mcaserta/1391757 to your computer and use it in GitHub Desktop.
A simple logging aspect
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Around("execution(public * com.nexse.swat.demo..*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable {
final Logger log = LoggerFactory.getLogger(pjp.getTarget().getClass());
final Object[] args = pjp.getArgs();
final String methodName = pjp.getSignature().getName();
if (!isUtilMethod(methodName)) {
log.debug("{}(): {}", methodName, args);
}
final Object result = pjp.proceed();
if (!isUtilMethod(methodName)) {
log.debug("{}(): result={}", methodName, result);
}
return result;
}
private boolean isUtilMethod(String name) {
return name.startsWith("get")
|| name.startsWith("set")
|| name.equals("toString")
|| name.equals("equals")
|| name.equals("hashCode");
}
}
@mcaserta
Copy link
Author

To see an example of this class in action, have a look at

https://github.com/mcaserta/aop-logging-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment