This commit is contained in:
张良(004796)
2024-01-17 15:26:20 +08:00
parent a6876dabb4
commit 2903719ea6
21 changed files with 134 additions and 19 deletions

View File

@@ -67,32 +67,36 @@ public class LogAspect {
protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
try {
// *========数据库日志=========*//
OperLogEvent operLog = new OperLogEvent();
StringBuilder logString = new StringBuilder();
logString.append("record logs:");
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
// 请求的地址
String ip = ServletUtils.getClientIP();
operLog.setOperIp(ip);
logString.append(String.format("ip=%s;",ip));
operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
LoginUser loginUser = LoginHelper.getLoginUser();
if(loginUser != null){
operLog.setOperName(loginUser.getUsername());
operLog.setUserType(loginUser.getUserType());
operLog.setDeptName(loginUser.getDeptName());
}
if (e != null) {
operLog.setStatus(BusinessStatus.FAIL.ordinal());
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
}
// 设置方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
operLog.setMethod(className + "." + methodName + "()");
// 设置请求方式
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
logString.append(String.format("url=%s;method=%s;title=%s;",operLog.getOperUrl(),operLog.getRequestMethod(),controllerLog.title()));
LoginUser loginUser = LoginHelper.getLoginUser();
if(loginUser != null){
operLog.setOperName(loginUser.getUsername());
operLog.setUserType(loginUser.getUserType());
operLog.setDeptName(loginUser.getDeptName());
}
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult,logString);
if (e != null) {
operLog.setStatus(BusinessStatus.FAIL.ordinal());
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
logString.append(String.format("exception=%s;",e.getMessage()));
}
// 发布事件保存数据库
SpringUtils.context().publishEvent(operLog);
} catch (Exception exp) {
@@ -109,7 +113,7 @@ public class LogAspect {
* @param operLog 操作日志
* @throws Exception
*/
public void getControllerMethodDescription(JoinPoint joinPoint, Log log, OperLogEvent operLog, Object jsonResult) throws Exception {
public void getControllerMethodDescription(JoinPoint joinPoint, Log log, OperLogEvent operLog, Object jsonResult,StringBuilder logString) throws Exception {
// 设置action动作
operLog.setBusinessType(log.businessType().ordinal());
// 设置标题
@@ -119,11 +123,13 @@ public class LogAspect {
// 是否需要保存request参数和值
if (log.isSaveRequestData()) {
// 获取参数的信息,传入到数据库中。
setRequestValue(joinPoint, operLog, log.excludeParamNames());
setRequestValue(joinPoint, operLog, log.excludeParamNames(),logString);
}
// 是否需要保存response参数和值
if (log.isSaveResponseData() && ObjectUtil.isNotNull(jsonResult)) {
operLog.setJsonResult(StringUtils.substring(JsonUtils.toJsonString(jsonResult), 0, 2000));
String jsonResultString = JsonUtils.toJsonString(jsonResult);
operLog.setJsonResult(StringUtils.substring(jsonResultString, 0, 2000));
logString.append(String.format("result=%s;",jsonResultString));
}
}
@@ -133,17 +139,20 @@ public class LogAspect {
* @param operLog 操作日志
* @throws Exception 异常
*/
private void setRequestValue(JoinPoint joinPoint, OperLogEvent operLog, String[] excludeParamNames) throws Exception {
private void setRequestValue(JoinPoint joinPoint, OperLogEvent operLog, String[] excludeParamNames,StringBuilder logString) throws Exception {
Map<String, String> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
String requestMethod = operLog.getRequestMethod();
if (MapUtil.isEmpty(paramsMap)
&& HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames);
operLog.setOperParam(StringUtils.substring(params, 0, 2000));
logString.append(String.format("params=%s;",params));
} else {
MapUtil.removeAny(paramsMap, EXCLUDE_PROPERTIES);
MapUtil.removeAny(paramsMap, excludeParamNames);
operLog.setOperParam(StringUtils.substring(JsonUtils.toJsonString(paramsMap), 0, 2000));
String params = JsonUtils.toJsonString(paramsMap);
operLog.setOperParam(StringUtils.substring(params, 0, 2000));
logString.append(String.format("params=%s;",params));
}
}