Spring AOP 实现日志记录
程序员文章站
2022-07-15 11:11:26
...
/**
* 通过Aop记录日志
*/
@Aspect
@Component
public class LogRecordAop {
@Autowired
private ISysLogService logService;
private long startTimeMillis = 0;
private long endTimeMillis = 0;
//函数执行前
@Before(value = "( execution(* com.spring.admin.controller.*.*.*.*(..))" +
"|| execution(* com.spring.admin.controller.*.*.*(..)) )")
public void before(JoinPoint joinPoint) {
startTimeMillis = System.currentTimeMillis();
}
//函数返回数据后
@AfterReturning(returning = "retVal", pointcut = "( execution(* com.spring.admin.controller.*.*.*.*(..))" +
"|| execution(* com.spring.admin.controller.*.*.*(..)) )")
public void afterReturning(JoinPoint joinPoint, Object retVal) {
try {
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
if (StringUtils.isNotBlank(className) || StringUtils.isNotBlank(methodName)) {
className = className.substring(className.lastIndexOf(".") + 1);
String operaType = "SELECT";
if (methodName.toLowerCase().indexOf("select") != -1) {
operaType = "SELECT";
} else if (methodName.toLowerCase().indexOf("insert") != -1) {
operaType = "INSERT";
} else if (methodName.toLowerCase().indexOf("update") != -1) {
operaType = "UPDATE";
} else if (methodName.toLowerCase().indexOf("delete") != -1) {
operaType = "DELETE";
} else if (methodName.toLowerCase().indexOf("login") != -1) {
operaType = "LOGIN";
} else if (methodName.toLowerCase().indexOf("send") != -1) {
operaType = "SEND";
}
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
String requestURL = request.getRequestURL().toString();
String requestType = request.getMethod();
String queryString = "";
Enumeration enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String paraName = (String) enu.nextElement();
String paramValue = request.getParameter(paraName);
queryString = queryString + paraName + "=" + paramValue + "&";
}
if (StringUtils.isNotBlank(queryString)) {
queryString = queryString.substring(0, queryString.length() - 1);
}
String token = "NULL";
String userType = "WEB";
if (requestURL.toLowerCase().indexOf("/admin/") != -1) {
token = request.getHeader("Authorization");
userType = "WEB";
} else if (requestURL.toLowerCase().indexOf("/api/") != -1) {
token = "API";
userType = "API";
} else if (requestURL.toLowerCase().indexOf("/app/") != -1) {
token = request.getParameter("token");
userType = "APP";
} else if (requestURL.toLowerCase().indexOf("/mp/") != -1) {
token = request.getParameter("openId");
userType = "WEIXIN";
}
TipDto td = (TipDto) retVal;
String message = null;
String devMessage = null;
String status = "0";
String result = null;
if (td != null) {
message = td.getMessage();
devMessage = td.getDevMessage();
status = String.valueOf(td.getStatus());
if (!"SELECT".equals(operaType)) {
result = JSON.toJSON(td).toString();
} else {
try {
Map<String, Object> dataMap = (Map<String, Object>) ((JSONObject) JSON.toJSON(td)).get("data");
if (dataMap == null || dataMap.size() == 0) {
result = "NULL";
} else {
List<Map<String, Object>> list = (List<Map<String, Object>>) dataMap.get("list");
if (list == null || list.size() == 0) {
result = JSON.toJSON(td).toString();
} else {
result = "NULL";
}
}
} catch (Exception e) {
}
}
}
endTimeMillis = System.currentTimeMillis();
Long runTime = endTimeMillis - startTimeMillis;
Map<String, Object> logMap = new HashMap<>();
logMap.put("token", StringUtils.isNotBlank(token) ? token : "NULL");
logMap.put("userType", StringUtils.isNotBlank(userType) ? userType : "NULL");
logMap.put("requestType", StringUtils.isNotBlank(requestType) ? requestType : "NULL");
logMap.put("requestURL", StringUtils.isNotBlank(requestURL) ? requestURL : "NULL");
logMap.put("className", StringUtils.isNotBlank(className) ? className : "NULL");
logMap.put("methodName", StringUtils.isNotBlank(methodName) ? methodName : "NULL");
logMap.put("operaType", StringUtils.isNotBlank(operaType) ? operaType : "NULL");
logMap.put("param", StringUtils.isNotBlank(queryString) ? queryString : "NULL");
logMap.put("runTime", StringUtils.isNotBlank(runTime.toString()) ? runTime : "NULL");
logMap.put("status", StringUtils.isNotBlank(status) ? status : "NULL");
logMap.put("message", StringUtils.isNotBlank(message) ? message : "NULL");
logMap.put("devMessage", StringUtils.isNotBlank(devMessage) ? devMessage : "NULL");
logMap.put("result", StringUtils.isNotBlank(result) ? result : "NULL");
logMap.put("logDate", DateUtil.getTime());
logMap.put("remark", "NULL");
this.logService.insertLog(logMap);
}
} catch (Exception e) {
}
return;
}
}
下一篇: Spring AOP实现日志记录