1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| package com.holelin.sundry.handler;
import com.alibaba.fastjson.JSON; import com.holelin.sundry.annotation.Log; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; 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.springframework.stereotype.Component; import org.springframework.util.StopWatch; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Objects; import java.util.UUID;
@Aspect @Slf4j @Component public class LogHandler {
@Pointcut("@annotation(com.holelin.sundry.annotation.Log)") public void logPointcut() { }
@Before("logPointcut()") public void doBefore(JoinPoint joinPoint) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String methodDetailDescription = this.getAspectMethodLogDesc(joinPoint); log.info("------------------------------- start --------------------------");
log.info("Method detail Description: {}", methodDetailDescription);
log.info("Request Args: {}", JSON.toJSONString(joinPoint.getArgs()));
log.info("Request method: {}", request.getMethod());
log.info("Request URL: {}", request.getRequestURL().toString());
log.info("Request Class and Method: {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); }
@Around("logPointcut()") public Object doAround(ProceedingJoinPoint point) throws Throwable { String aspectMethodLogDescPJ = getAspectMethodLogDesc(point); StopWatch stopWatch = new StopWatch(UUID.randomUUID().toString()); stopWatch.start(); final Object result = point.proceed(); stopWatch.stop(); log.info("{} Response result: {}", aspectMethodLogDescPJ, JSON.toJSONString(result)); log.info("------------------------------- end ------------------------------"); return result; }
@AfterReturning(returning = "ref", pointcut = "logPointcut()") public void doAfterReturning(JoinPoint point, Object ref) { final Method method = getMethod(point); if (Objects.nonNull(method.getAnnotation(Log.class))) { log.info("doAfterReturning,Object:{}", ref); } }
@AfterThrowing("logPointcut()") public void doAfterThrowing(JoinPoint point) { final Method method = getMethod(point); if (Objects.nonNull(method.getAnnotation(Log.class))) { log.info("doAfterThrowing"); } }
private Method getMethod(JoinPoint point) { final MethodSignature signature = (MethodSignature) point.getSignature(); return signature.getMethod(); }
public String getAspectMethodLogDesc(JoinPoint joinPoint) throws Exception { String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] arguments = joinPoint.getArgs(); return getAspectMethodLogDesc(targetName, methodName, arguments); }
public String getAspectMethodLogDesc(ProceedingJoinPoint proceedingJoinPoint) throws Exception { String targetName = proceedingJoinPoint.getTarget().getClass().getName(); String methodName = proceedingJoinPoint.getSignature().getName(); Object[] arguments = proceedingJoinPoint.getArgs(); return getAspectMethodLogDesc(targetName, methodName, arguments); }
public String getAspectMethodLogDesc(String targetName, String methodName, Object[] arguments) throws Exception { Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); StringBuilder description = new StringBuilder(""); for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { description.append(method.getAnnotation(Log.class).description()); break; } } } return description.toString(); } }
|