Created
June 2, 2020 18:38
-
-
Save akashchandwani/1284ff8efc0634c56d981769fa9d17d6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# package com.example.demo.aspect; | |
import org.aspectj.lang.JoinPoint; | |
import org.aspectj.lang.Signature; | |
import org.aspectj.lang.annotation.AfterReturning; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.aspectj.lang.annotation.Before; | |
import org.aspectj.lang.annotation.Pointcut; | |
import org.aspectj.lang.reflect.MethodSignature; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.stereotype.Component; | |
@Aspect | |
@Component | |
public class LoggerAspect { | |
private Logger log; | |
@Pointcut("execution(public * com.example.demo.service..*(..))") | |
public void allMethods() { | |
} | |
@Before("allMethods()") | |
public void before(JoinPoint joinPoint) { | |
log = LoggerFactory.getLogger(joinPoint.getTarget().getClass()); | |
log.trace("[Enter : {}] with args {}", joinPoint.getSignature().toShortString(), joinPoint.getArgs()); | |
} | |
@AfterReturning(pointcut = "allMethods()", returning = "returnValue") | |
public void after(JoinPoint joinPoint, Object returnValue) { | |
log = LoggerFactory.getLogger(joinPoint.getTarget().getClass()); | |
Signature signature = joinPoint.getSignature(); | |
log.trace("[Exit : {}] with return type [{}] and value [{}]", signature.toShortString(), | |
((MethodSignature) signature).getReturnType(), returnValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment