RunAspect.java
7.6 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.dituhui.pea.aop;
import com.alibaba.fastjson.JSONObject;
import com.dituhui.pea.enums.StatusCodeEnum;
import com.dituhui.pea.exception.BusinessException;
import com.dituhui.pea.log.LogsAnnotation;
import com.dituhui.pea.log.PlatformLogger;
import com.dituhui.pea.pojo.CurrentUser;
import com.dituhui.pea.pojo.PlatformUser;
import com.dituhui.pea.pojo.WebResult;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Calendar;
/**
* 处理日志
*/
@Aspect
@Component
@Order(1)
public class RunAspect {
/**
* 日志级别
*/
private final static String ri = "info", re = "error";
@Autowired
private PlatformLogger platformLogger;
@Autowired
private CurrentUser currentUser;
/**
* 参数最大长度
*/
private final static int argLen = 3000;
/**
* 控制层运行日志
*
* @param p 切面点
* @return obj
*/
@Deprecated
@Around("execution(* com.dituhui.pea.*.controller.*.*(..))")
public Object webLog(ProceedingJoinPoint p) {
// 请求参数
StringBuilder sb = new StringBuilder();
try {
// 记录参数类型及值
for (int i = 0; i < p.getArgs().length; i++) {
try {
if (p.getArgs()[i] != null && StringUtils.isNotEmpty(p.getArgs()[i].getClass().getSimpleName())) {
// 存在参数类型才记录
sb.append(",");
sb.append(p.getArgs()[i].getClass().getSimpleName());
sb.append(":");
sb.append(p.getArgs()[i]);
}
} catch (Exception e) {
// 参数获取失败
sb.append("args error(").append(p.getArgs().length).append("):").append(i + 1);
}
}
} catch (Exception e) {
// 参数获取失败
sb.append("args error(").append(p.getArgs().length).append("):未知错误");
}
PlatformUser user = currentUser.getPlatformUser();
// 返回类型
Class<?> returnType = null;
// 日志对象
JSONObject jObj = new JSONObject();
if (sb.toString().length() > 0) {
// 记录参数,如果超出长度则截取
if (sb.length() > argLen) {
jObj.put("params", sb.substring(1, argLen) + "...");
} else {
jObj.put("params", sb.substring(1));
}
}
jObj.put("bID", user.getBid());
if (StringUtils.isNotEmpty(user.getToken())) {
jObj.put("tk", user.getToken());
}
jObj.put("sourceIp", user.getClientIP());
jObj.put("t", user.getT());
// 操作时间,operation time
jObj.put("ot", Calendar.getInstance().getTime());
// 日志开始时间
long beginTime = System.currentTimeMillis();
try {
MethodSignature ms = (MethodSignature) p.getSignature();
returnType = ms.getReturnType();
Object result = p.proceed();
jObj.put("level", ri);
// 耗时,time counting
jObj.put("elapse", System.currentTimeMillis() - beginTime);
platformLogger.info(ms.getMethod(), jObj);
currentUser.removePlatformUser();
return result;
} catch (Throwable t) {
jObj.put("level", re);
jObj.put("elapse", System.currentTimeMillis() - beginTime);
platformLogger.error(t, jObj);
currentUser.removePlatformUser();
// 判断是否为业务异常
if (t instanceof BusinessException) {
StatusCodeEnum codeEnum = ((BusinessException) t).codeEnum;
return WebResult.failed(codeEnum);
} else {
if (returnType != null && WebResult.class.getSimpleName().equals(returnType.getSimpleName())) {
// 指定返回类型
return WebResult.failed(StatusCodeEnum.SYSTEM_UNKNOWN_ERROR);
}
}
}
currentUser.removePlatformUser();
return WebResult.failed(StatusCodeEnum.SYSTEM_UNKNOWN_ERROR);
}
/**
* 服务模块运行日志
*
* @param p 切面点
* @return obj
* @throws Throwable throwable
*/
@Around("execution(* com.dituhui.pea.*.service.*.*(..)) || execution(* com.dituhui.pea.*.service.*.service.impl.*.*(..))"
+ "||execution(* com.dituhui.pea.*.service.*.custom.service.*.*(..))")
public Object serviceLog(ProceedingJoinPoint p) throws Throwable {
PlatformUser user = currentUser.getPlatformUser();
// 请求参数
StringBuilder sb = new StringBuilder();
try {
// 记录参数类型及值
for (int i = 0; i < p.getArgs().length; i++) {
try {
if (p.getArgs()[i] != null && StringUtils.isNotEmpty(p.getArgs()[i].getClass().getSimpleName())) {
// 存在参数类型才记录
sb.append(",");
sb.append(p.getArgs()[i].getClass().getSimpleName());
sb.append(":");
sb.append(p.getArgs()[i]);
}
} catch (Exception e) {
// 参数获取失败
sb.append("args error(").append(p.getArgs().length).append("):").append(i + 1);
}
}
} catch (Exception e) {
// 参数获取失败
sb.append("args error(").append(p.getArgs().length).append("):未知错误");
}
// 日志对象
JSONObject jObj = new JSONObject();
if (sb.toString().length() > 0) {
// 记录参数,如果超出长度则截取
if (sb.length() > argLen) {
jObj.put("params", sb.substring(1, argLen) + "...");
} else {
jObj.put("params", sb.substring(1));
}
}
jObj.put("bID", user.getBid());
if (StringUtils.isNotEmpty(user.getToken())) {
jObj.put("tk", user.getToken());
}
jObj.put("sourceIp", user.getClientIP());
jObj.put("t", user.getT());
// 操作时间,operation time
jObj.put("ot", Calendar.getInstance().getTime());
// 日志开始时间
long beginTime = System.currentTimeMillis();
MethodSignature ms = (MethodSignature) p.getSignature();
Method method = ms.getMethod();
LogsAnnotation logsAnnotation = method.getAnnotation(LogsAnnotation.class);
Object result;
try {
result = p.proceed();
} catch (Throwable t) {
jObj.put("level", re);
jObj.put("elapse", System.currentTimeMillis() - beginTime);
platformLogger.error(t, jObj, method);
// 异常由最上层捕获,所以向上抛
throw t;
}
jObj.put("level", ri);
// 耗时,time counting
jObj.put("elapse", System.currentTimeMillis() - beginTime);
if (logsAnnotation == null || logsAnnotation.logSwitch()) {
// 根据日志开关决定是否记录日志
platformLogger.info(method, jObj);
}
return result;
}
}