|
| 1 | +package com.quick.component.config; |
| 2 | + |
| 3 | +import cn.hutool.core.util.StrUtil; |
| 4 | +import cn.hutool.core.util.URLUtil; |
| 5 | +import cn.hutool.json.JSONUtil; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 8 | +import org.aspectj.lang.Signature; |
| 9 | +import org.aspectj.lang.annotation.Around; |
| 10 | +import org.aspectj.lang.annotation.Aspect; |
| 11 | +import org.aspectj.lang.reflect.MethodSignature; |
| 12 | +import org.springframework.util.StopWatch; |
| 13 | +import org.springframework.util.StringUtils; |
| 14 | +import org.springframework.web.bind.annotation.RequestBody; |
| 15 | +import org.springframework.web.bind.annotation.RequestParam; |
| 16 | +import org.springframework.web.context.request.RequestContextHolder; |
| 17 | +import org.springframework.web.context.request.ServletRequestAttributes; |
| 18 | + |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Parameter; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +@Aspect |
| 28 | +@Slf4j |
| 29 | +public class WebLogAspect { |
| 30 | + |
| 31 | +// @Pointcut("execution(public * com.quick.log..controller.*.*(..))")//两个..代表所有子目录,最后括号里的两个..代表所有参数 |
| 32 | +// @Pointcut("${pointcut.value}")//两个..代表所有子目录,最后括号里的两个..代表所有参数 |
| 33 | +// @Pointcut("#{@value}")//两个..代表所有子目录,最后括号里的两个..代表所有参数 |
| 34 | +// public void logPointCut() { |
| 35 | +// } |
| 36 | +// |
| 37 | +// @Before("logPointCut()") |
| 38 | +// public void doBefore(JoinPoint joinPoint) throws Throwable { |
| 39 | +// } |
| 40 | +// |
| 41 | +// @AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning的值和doAfterReturning的参数名一致 |
| 42 | +// public void doAfterReturning(Object ret) throws Throwable { |
| 43 | +// } |
| 44 | + |
| 45 | +// @Around("logPointCut()") |
| 46 | + @Around("execution(public * com..controller.*.*(..))") |
| 47 | + public Object doAround(ProceedingJoinPoint pjp) throws Throwable { |
| 48 | + //获取当前请求对象 |
| 49 | + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| 50 | + HttpServletRequest request = attributes.getRequest(); |
| 51 | + //记录请求信息 |
| 52 | + StopWatch stopWatch = new StopWatch(); |
| 53 | + stopWatch.start(); |
| 54 | + Object result = pjp.proceed(); |
| 55 | + stopWatch.stop(); |
| 56 | + |
| 57 | + Signature signature = pjp.getSignature(); |
| 58 | + MethodSignature methodSignature = (MethodSignature) signature; |
| 59 | + Method method = methodSignature.getMethod(); |
| 60 | + String urlStr = request.getRequestURL().toString(); |
| 61 | + log.info("===================================begin=================================================="); |
| 62 | + log.info("req path: {}", StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath())); |
| 63 | + log.info("req ip: {}", request.getRemoteUser()); |
| 64 | + log.info("req method: {}", request.getMethod()); |
| 65 | + log.info("req params: {}", getParameter(method, pjp.getArgs())); |
| 66 | + log.info("req result: {}", JSONUtil.toJsonStr(result)); |
| 67 | + log.info("req uri: {}", request.getRequestURI()); |
| 68 | + log.info("req url: {}", request.getRequestURL().toString()); |
| 69 | + log.info("req cost: {}ms", stopWatch.getLastTaskTimeMillis()); |
| 70 | + log.info("===================================end==================================================="); |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * 根据方法和传入的参数获取请求参数 |
| 77 | + */ |
| 78 | + private Object getParameter(Method method, Object[] args) { |
| 79 | + List<Object> argList = new ArrayList<>(); |
| 80 | + Parameter[] parameters = method.getParameters(); |
| 81 | + for (int i = 0; i < parameters.length; i++) { |
| 82 | + //将RequestBody注解修饰的参数作为请求参数 |
| 83 | + RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); |
| 84 | + if (requestBody != null) { |
| 85 | + argList.add(args[i]); |
| 86 | + } |
| 87 | + //将RequestParam注解修饰的参数作为请求参数 |
| 88 | + RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); |
| 89 | + if (requestParam != null) { |
| 90 | + Map<String, Object> map = new HashMap<>(); |
| 91 | + String key = parameters[i].getName(); |
| 92 | + if (!StringUtils.isEmpty(requestParam.value())) { |
| 93 | + key = requestParam.value(); |
| 94 | + } |
| 95 | + map.put(key, args[i]); |
| 96 | + argList.add(map); |
| 97 | + } |
| 98 | + } |
| 99 | + if (argList.size() == 0) { |
| 100 | + return null; |
| 101 | + } else if (argList.size() == 1) { |
| 102 | + return argList.get(0); |
| 103 | + } else { |
| 104 | + return argList; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments