|
| 1 | +package net.guides.springboot2.springboot2jpacrudexample.aspect; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +import org.aspectj.lang.JoinPoint; |
| 6 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 7 | +import org.aspectj.lang.annotation.After; |
| 8 | +import org.aspectj.lang.annotation.AfterReturning; |
| 9 | +import org.aspectj.lang.annotation.AfterThrowing; |
| 10 | +import org.aspectj.lang.annotation.Around; |
| 11 | +import org.aspectj.lang.annotation.Aspect; |
| 12 | +import org.aspectj.lang.annotation.Before; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | + |
| 17 | +/** |
| 18 | + * Aspect for logging execution. |
| 19 | + * |
| 20 | + * @author Ramesh Fadatare |
| 21 | + * |
| 22 | + */ |
| 23 | +@Aspect |
| 24 | +@Component |
| 25 | +public class LoggingAspect { |
| 26 | + |
| 27 | + private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 28 | + |
| 29 | + /** |
| 30 | + * Run before the method execution. |
| 31 | + * @param joinPoint |
| 32 | + */ |
| 33 | + @Before("execution(* net.guides.springboot2.springboot2jpacrudexample.service.EmployeeService.addEmployee(..))") |
| 34 | + public void logBefore(JoinPoint joinPoint) { |
| 35 | + log.debug("logBefore running ....."); |
| 36 | + log.debug("Enter: {}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(), |
| 37 | + joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs())); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Run after the method returned a result. |
| 43 | + * @param joinPoint |
| 44 | + */ |
| 45 | + @After("execution(* net.guides.springboot2.springboot2jpacrudexample.service.EmployeeService.addEmployee(..))") |
| 46 | + public void logAfter(JoinPoint joinPoint) { |
| 47 | + log.debug("logAfter running ....."); |
| 48 | + log.debug("Enter: {}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(), |
| 49 | + joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs())); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Run after the method returned a result, intercept the returned result as well. |
| 54 | + * @param joinPoint |
| 55 | + * @param result |
| 56 | + */ |
| 57 | + @AfterReturning(pointcut = "execution(* net.guides.springboot2.springboot2jpacrudexample.service.EmployeeService.deleteEmployee(..))", returning = "result") |
| 58 | + public void logAfterReturning(JoinPoint joinPoint, Object result) { |
| 59 | + log.debug("logAfterReturning running ....."); |
| 60 | + log.debug("Enter: {}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(), |
| 61 | + joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs())); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Run around the method execution. |
| 67 | + * @param joinPoint |
| 68 | + * @return |
| 69 | + * @throws Throwable |
| 70 | + */ |
| 71 | + @Around("execution(* net.guides.springboot2.springboot2jpacrudexample.service.EmployeeService.getEmployeeById(..))") |
| 72 | + public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { |
| 73 | + log.debug("logAround running ....."); |
| 74 | + if (log.isDebugEnabled()) { |
| 75 | + log.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(), |
| 76 | + joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs())); |
| 77 | + } |
| 78 | + try { |
| 79 | + Object result = joinPoint.proceed(); |
| 80 | + if (log.isDebugEnabled()) { |
| 81 | + log.debug("Exit: {}.{}() with result = {}", joinPoint.getSignature().getDeclaringTypeName(), |
| 82 | + joinPoint.getSignature().getName(), result); |
| 83 | + } |
| 84 | + return result; |
| 85 | + } catch (IllegalArgumentException e) { |
| 86 | + log.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()), |
| 87 | + joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); |
| 88 | + throw e; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Advice that logs methods throwing exceptions. |
| 95 | + * |
| 96 | + * @param joinPoint join point for advice |
| 97 | + * @param e exception |
| 98 | + */ |
| 99 | + |
| 100 | + @AfterThrowing(pointcut = "execution(* net.guides.springboot2.springboot2jpacrudexample.service.EmployeeService.updateEmployee(..))", throwing = "error") |
| 101 | + public void logAfterThrowing(JoinPoint joinPoint, Throwable error) { |
| 102 | + log.debug("logAfterThrowing running ....."); |
| 103 | + log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), |
| 104 | + joinPoint.getSignature().getName(), error.getCause() != null ? error.getCause() : "NULL"); |
| 105 | + } |
| 106 | +} |
0 commit comments