LogPrintAop.java
3.42 KB
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
package com.diligrp.cashier.boss.aop;
import com.diligrp.cashier.shared.annotation.ParamLogPrint;
import com.diligrp.cashier.shared.util.JsonUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.BooleanUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
/**
* @author dengwei
* @version 1.0.0
* @ClassName LogPrintAspect.java
* @Description 方法参数打印切面
*/
@Component
@Aspect
@Order(0)
public class LogPrintAop {
private static final Logger log = LoggerFactory.getLogger(LogPrintAop.class);
private static final String VERSION = "feat_1.0.0";
/**
* 参数
*/
@Before("@annotation(commonParamLogPrint)")
public void paramLogPrint(JoinPoint joinPoint, ParamLogPrint commonParamLogPrint) {
try {
boolean print = commonParamLogPrint.print();
if (!print) {
return;
}
Signature signature = joinPoint.getSignature();
// 使用下述方法需要开启--enable-preview预览功能
// String className = STR."\{signature.getDeclaringTypeName()}.\{signature.getName()}";
String className = signature.getDeclaringTypeName() + "." + signature.getName();
Object[] args = joinPoint.getArgs();
List<Object> argsList = this.listArgs(args);
Map<String, Object> logInfo = new LinkedHashMap<>(2);
logInfo.put("version", VERSION);
logInfo.put("method", className);
logInfo.put("args", argsList);
log.info("{}:{}", commonParamLogPrint.desc(), JsonUtils.toJsonString(logInfo));
} catch (Exception exception) {
log.warn("param log error");
}
}
/**
* 处理完请求后执行
*/
@AfterReturning(pointcut = "@annotation(print)", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint, ParamLogPrint print, Object jsonResult) {
try {
if (BooleanUtils.isTrue(print.outPrint())) {
log.warn("{} rsp:{}", print.desc(), JsonUtils.toJsonString(jsonResult));
}
} catch (Exception e) {
log.warn("after return error!");
}
}
/**
* 参数列表
*
* @param args args
* @return {@link List}<{@link Object}>
*/
private List<Object> listArgs(Object[] args) {
List<Object> argsList = new LinkedList<>();
for (Object arg : args) {
Object param;
if (arg instanceof HttpServletResponse) {
param = HttpServletResponse.class.getSimpleName();
} else if (arg instanceof HttpServletRequest) {
param = HttpServletRequest.class.getSimpleName();
} else if (arg instanceof MultipartFile) {
param = MultipartFile.class.getSimpleName();
} else {
param = arg;
}
if (Objects.nonNull(param)) {
argsList.add(param);
}
}
return argsList;
}
}