SysLogAspect.java
4.99 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
package cn.com.glsx.merchants.config;
import cn.com.glsx.merchants.contants.Contants;
import cn.com.glsx.merchants.model.response.MerchantLogResponse;
import cn.com.glsx.merchants.request.log.LogRequest;
import cn.com.glsx.merchants.service.MerchantLogService;
import cn.com.glsx.merchants.service.MerchantUserService;
import com.glsx.plat.common.annotation.SysLog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.rop.AbstractRopRequest;
import com.rop.session.SimpleSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.oreframework.boot.security.cas.User;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author zhouhaibao
* @version 1.0
* @description: 操作日志统一处理
* @date 2021/7/13 10:00
*/
@Slf4j
@Aspect
@Component
public class SysLogAspect {
final static GsonBuilder builder = new GsonBuilder();
final static Gson gson = builder.create();
@Autowired
private MerchantLogService merchantLogService;
@Autowired
private MerchantUserService merchantUserService;
@Pointcut("@annotation(com.glsx.plat.common.annotation.SysLog)")
public void logPointCut() {
}
@Before("logPointCut()")
public void webpre(JoinPoint joinPoint) {
// 处理日志
try {
saveSysLog(joinPoint);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印方法执行时间
*/
@Around("logPointCut()")
public Object timeAround(ProceedingJoinPoint jp) throws Throwable {
long beginTime = System.currentTimeMillis();
//执行方法,执行失败后会抛出异常,也就不会打印执行时间
Object result = jp.proceed();
//执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
MethodSignature methodSignature = (MethodSignature) jp.getSignature();
Object target = jp.getTarget();
Method method = methodSignature.getMethod();
//获取注解上的文字
SysLog sysLog = method.getAnnotation(SysLog.class);
String methodName = methodSignature.getName();
if (sysLog.printSpendTime()) {
log.info(target.getClass().getName() + "." + methodName + " 执行耗时:" + DurationFormatUtils.formatDuration(time, "HH:mm:ss.S") + "; 精确时间为 :" + time + " ms");
}
return result;
}
/**
* 方法之后调用
*
* @param jp
* @param returnValue 方法返回值
*/
@AfterReturning(pointcut = "logPointCut()", returning = "returnValue")
public void doAfterReturning(JoinPoint jp, Object returnValue) {
log.info("Returning Args : {}", new Gson().toJson(returnValue));
// 处理完请求,返回内容
String logTraceId = MDC.get(Contants.MDC_LOG_DB_ID);
// 处理完请求,返回内容
/* RpcResponse rpcResponse=(RpcResponse)returnValue;
if(rpcResponse.isFailed()){
LogRequest logRequest = new LogRequest();
logRequest.setResult("失败");
logRequest.setId(Integer.parseInt(logTraceId));
merchantLogService.updateMerchantLog(logRequest);
}*/
}
/**
* 供后台和基础服务调用系统日志保存
*
* @param joinPoint
*/
public void saveSysLog(JoinPoint joinPoint) throws Exception {
//获取方法信息
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
// 获取accessToken的下表
Method method = methodSignature.getMethod();
// 日志标识
SysLog sysLogMark = method.getAnnotation(SysLog.class);
// 开始打印请求日志
// 获取 @WebLog 注解的描述信息
// 打印请求入参
Object[] args = joinPoint.getArgs();
AbstractRopRequest accessToken = (AbstractRopRequest) args[0];
String ip = accessToken.getRopRequestContext().getIp();
System.out.println(args);
// 插入日志
LogRequest logRequest = new LogRequest();
logRequest.setModule(sysLogMark.module());
logRequest.setAction(sysLogMark.value());
User user = merchantUserService.getUser(accessToken);
logRequest.setUserName(user.getRealname());
logRequest.setOperator(user.getUsername());
logRequest.setIp(ip);
logRequest.setResult("成功");
MerchantLogResponse merchantLogResponse = merchantLogService.saveMerchantLog(logRequest);
if(merchantLogResponse!=null){
MDC.put(Contants.MDC_LOG_DB_ID, merchantLogResponse.getId()+"");
}
}
}