Skip to content

Instantly share code, notes, and snippets.

@tonypiazza
Created September 25, 2012 20:36
Show Gist options
  • Save tonypiazza/3784284 to your computer and use it in GitHub Desktop.
Save tonypiazza/3784284 to your computer and use it in GitHub Desktop.
This aspect demonstrates how to implement logging of method and constructor entry/exit. This is something I created recently for a client to replace more than 10,000 lines of logging code.
package com.piazzaconsulting.aspect;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MemberSignature;
public aspect LoggingAspect {
private static final String POINTCUT =
"execution(* com.yourcompany.whatever..*(..)) || initialization(com.yourcompany.whatever..new(..))";
@Before(POINTCUT)
public void logEntryAdvice(JoinPoint joinPoint) {
MemberSignature signature = (MemberSignature)joinPoint.getSignature();
Logger logger = Logger.getLogger(signature.getDeclaringTypeName());
logger.trace(">>> " + signature.getName());
}
@After(POINTCUT)
public void logExitAdvice(JoinPoint joinPoint) {
MemberSignature signature = (MemberSignature)joinPoint.getSignature();
Logger logger = Logger.getLogger(signature.getDeclaringTypeName());
logger.trace("<<< " + signature.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment