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
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.AspectLogBody;
import com.yanzuoguang.log.LogCountTime;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import org.springframework.stereotype.Component;
/**
* 写入日志
*
* @author 颜佐光
*/
@Component
public class AspectLogResult {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final AspectLogBody aspectLogBody;
private final LogCountTime logCountTime;
private final ResponseResult<String> responseDefault = ResponseResult.result("操作成功");
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, LogCountTime logCountTime) {
this.cloudConfig = cloudConfig;
this.logLocal = logLocal;
this.aspectLogBody = aspectLogBody;
this.logCountTime = logCountTime;
}
/**
* 保存日志
*
* @param cls 类型
* @param tag 标签名称
* @param url 地址
* @param logFlag 请求日志
* @param requestBody 请求内容
* @param result 结果
* @param resultEx 异常结果
* @param log 内容
*/
// @Async
public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, Object requestBody, Object result, Exception resultEx, LogVo log) {
// 执行时间
long time = System.currentTimeMillis() - log.getStart();
// 全路径
String fullUrl = String.format("%s:%s", tag, url);
try {
// 输出日志标记
boolean isLogCommon = logFlag && this.cloudConfig.isLogCommon();
boolean isError = resultEx != null;
boolean isLogDisplay = isLogCommon || isError;
// 记录请求时间
logCountTime.finish(fullUrl, time, isError);
// ThreadHelper.waitRun(WAIT_MAX, WAIT_ITEM, k -> StringHelper.isEmpty(log.getLogId()))
if (StringHelper.isEmpty(log.getLogId())) {
return;
}
if (!isLogDisplay) {
logLocal.remove(log);
return;
}
String body = aspectLogBody.getBodyString(requestBody);
if (!StringHelper.compare(log.getContent(), body)) {
log.setContentTo(body);
}
// 处理结果
ResponseResult<?> responseResult;
if (result instanceof ResponseResult) {
responseResult = (ResponseResult<?>) result;
} else if (result != null) {
responseResult = ResponseResult.result(result);
} else if (resultEx != null) {
responseResult = ExceptionHelper.getError(resultEx);
} else {
responseResult = responseDefault;
}
String json = JsonHelper.serialize(responseResult);
Log.error(cls, resultEx, "%s [ %s ] time %d ms, result: %s", tag, url, time, aspectLogBody.getMaxString(json));
logLocal.result(log, responseResult.getCode(), json, isError);
} catch (Exception e) {
ExceptionHelper.PrintError(AspectLogResult.class, e);
} finally {
if (logFlag) {
Log.threadCommit();
}
}
}
}