#
Junjie
1 天以前 253e9b3735533c4d8c69cf5e6983d5fa1e984ae9
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
package com.zy.common.config;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.asrs.entity.ApiLog;
import com.zy.asrs.service.ApiLogService;
import com.zy.common.annotations.OpenApiLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
@Component
@Aspect
@Slf4j
public class AspectConfig {
 
    @Autowired
    private ApiLogService apiLogService;
 
    @Pointcut("execution(* com.zy.asrs.controller.*(..))")
    private void webLog() {
    }
 
    @Around("@within(org.springframework.web.bind.annotation.RestController)" +
            "||@within(org.springframework.stereotype.Controller)")
    public Object after(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes != null ? requestAttributes.getRequest() : null;
        long start = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        OpenApiLog annotation = method.isAnnotationPresent(OpenApiLog.class) ? method.getAnnotation(OpenApiLog.class) : null;
        Object result = null;
        Object errorResponse = null;
        try {
            result = joinPoint.proceed(joinPoint.getArgs());
            if (annotation != null && !Cools.isEmpty(annotation.memo())) {
                saveLog(joinPoint, request, result, annotation.memo());
            }
            return result;
        } catch (Throwable ex) {
            errorResponse = buildErrorResponse(ex);
            if (annotation != null && !Cools.isEmpty(annotation.memo())) {
                saveErrLog(joinPoint, request, errorResponse, ex, annotation.memo());
            }
            throw ex;
        } finally {
            long end = System.currentTimeMillis();
//            log.info("请求日志的打印");
//            log.info("请求地址:{}", request != null ? Optional.ofNullable(request.getRequestURI()).orElse(null) : null);
//            log.info("请求方式:{}", request != null ? request.getMethod() : null);
//            log.info("请求类方法:{}", joinPoint.getSignature());
//            log.info("请求类方法参数:{}", JSONObject.toJSONString(filterArgs(joinPoint.getArgs())));
//            log.info("请求响应参数{}", JSONObject.toJSONString(result != null ? result : errorResponse));
//            log.info("执行耗时:{}", end - start);
        }
    }
 
    private List<Object> filterArgs(Object[] objects) {
        return Arrays.stream(objects).filter(obj -> !(obj instanceof MultipartFile)
                && !(obj instanceof HttpServletResponse)
                && !(obj instanceof HttpServletRequest)).collect(Collectors.toList());
    }
 
    private void saveLog(ProceedingJoinPoint joinPoint, HttpServletRequest request, Object result, String memo) {
        apiLogService.insert(new ApiLog(
                null,
                memo,
                request != null ? Optional.ofNullable(String.valueOf(request.getRequestURI())).orElse(null) : null,
                null,
                null,
                null,
                JSONObject.toJSONString(filterArgs(joinPoint.getArgs())),
                JSON.toJSONString(result),
                null,
                1,
                1,
                new Date(),
                null,
                null
        ));
    }
 
    private void saveErrLog(ProceedingJoinPoint joinPoint, HttpServletRequest request, Object response, Throwable ex, String memo) {
        apiLogService.insert(new ApiLog(
                null,
                memo,
                request != null ? Optional.ofNullable(String.valueOf(request.getRequestURI())).orElse(null) : null,
                null,
                null,
                null,
                JSONObject.toJSONString(filterArgs(joinPoint.getArgs())),
                JSON.toJSONString(response),
                String.valueOf(ex),
                0,
                1,
                new Date(),
                null,
                null
        ));
    }
 
    private Object buildErrorResponse(Throwable ex) {
        if (ex instanceof CoolException) {
            return R.parse(ex.getMessage());
        }
        return R.error();
    }
 
}