Created
April 15, 2020 15:55
-
-
Save stemlaur/e565a15c6ea9f9c0953bf20de41f6380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Aspect | |
public class LoggingAspect { | |
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingAspect.class); | |
public LoggingAspect() { | |
} | |
/** | |
* Pointcut that matches all Spring beans in the application's main packages. | |
*/ | |
@Pointcut("within(fr.bioserenity.domain.repository..*)" + | |
" || within(fr.bioserenity.service..*)" + | |
" || within(fr.bioserenity.blahblah..*)") | |
public void applicationPackagePointcut() { | |
// Method is empty as this is just a Pointcut, the implementations are in the advices. | |
} | |
/** | |
* Advice that logs when a method is entered and exited. | |
* | |
* @param joinPoint join point for advice | |
* @return result | |
* @throws Throwable throws IllegalArgumentException | |
*/ | |
@Around("applicationPackagePointcut()") | |
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { | |
LOGGER.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(), | |
joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs())); | |
try { | |
Object result = joinPoint.proceed(); | |
LOGGER.debug("Exit: {}.{}() with result = {}", joinPoint.getSignature().getDeclaringTypeName(), | |
joinPoint.getSignature().getName(), result); | |
return result; | |
} catch (IllegalArgumentException e) { | |
LOGGER.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()), | |
joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); | |
throw e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment