@Author: zhichong @Date: 2023/7/24 18:24 @Description: LogRecodeAspect @Version 1.0.0
| 41 | * @Version 1.0.0 |
| 42 | */ |
| 43 | @Aspect |
| 44 | @Component |
| 45 | @Slf4j |
| 46 | public class LogRecodeAspect { |
| 47 | @Value("${configuration.isEnableAopLog:false}") |
| 48 | private Boolean isEnableAopLog; |
| 49 | |
| 50 | @Value("${spring.application.name:notHave}") |
| 51 | private String springApplicationName; |
| 52 | |
| 53 | |
| 54 | @Autowired |
| 55 | private RedisTemplate<String, Object> redisTemplate; |
| 56 | |
| 57 | @Pointcut("execution(public * com.controller.*.*(..))") |
| 58 | public void controllerAspectse() { |
| 59 | } |
| 60 | |
| 61 | |
| 62 | @Around(value = "controllerAspectse()") |
| 63 | public Object controllerAspectse(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
| 64 | Object result = proceedingJoinPoint.proceed(); |
| 65 | if(isEnableAopLog){ |
| 66 | result = recordLog(proceedingJoinPoint); |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | public Object recordLog(ProceedingJoinPoint point) throws Throwable { |
| 71 | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| 72 | HttpServletRequest request = (HttpServletRequest) Objects.requireNonNull(attributes).getRequest(); |
| 73 | String methodName = point.getSignature().getName(); |
| 74 | Object result = point.proceed(); |
| 75 | if(methodName.startsWith("update")||methodName.startsWith("save")){ |
| 76 | // 打印请求相关参数 |
| 77 | long startTime = System.currentTimeMillis(); |
| 78 | String traceId = request.getAttribute("trace_id").toString(); |
| 79 | // final ControllerAspectseLog l = ControllerAspectseLog.builder() |
| 80 | // .ip(getIp(request)) |
| 81 | // .url(request.getRequestURL().toString()) |
| 82 | // .httpMethod(request.getMethod()) |
| 83 | // .requestParams(getNameAndValue(point)) |
| 84 | // .result(result) |
| 85 | // .timeCost(System.currentTimeMillis() - startTime) |
| 86 | // .traceId(traceId) |
| 87 | // .springApplicationName(springApplicationName) |
| 88 | // .build(); |
| 89 | // uploadData(l); |
| 90 | } |
| 91 | |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | // /** |
| 97 | // * 数据上传 |
| 98 | // * @param l |
| 99 | // */ |
| 100 | // private void uploadData(ControllerAspectseLog l) { |
nothing calls this directly
no outgoing calls
no test coverage detected