This commit is contained in:
USER-20220102CG\noblelift
2023-02-06 23:29:20 +08:00
parent 79e16a8e1d
commit a494d456c3
12 changed files with 139 additions and 121 deletions

View File

@@ -9,8 +9,8 @@ public enum InterfaceLogType {
DEFAULT("默认"),
LMS_TO_ACS("LMS请求ACS"),
ACS_TO_LMS("ACS请求LMS"),
ACS_TO_LK("ACS请求LMS"),
LK_TO_ACS("ACS请求LMS");
ACS_TO_LK("ACS请求立库"),
LK_TO_ACS("立库请求ACS");
private String desc;

View File

@@ -18,19 +18,15 @@ package org.nl.modules.logging.aspect;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.nl.common.utils.IdUtil;
import org.nl.modules.common.utils.RequestHolder;
import org.nl.modules.common.utils.SecurityUtils;
import org.nl.modules.common.utils.StringUtils;
import org.nl.modules.common.utils.ThrowableUtil;
import org.nl.modules.logging.domain.Log;
@@ -56,8 +52,6 @@ public class LogAspect {
private final LogService logService;
ThreadLocal<Long> currentTime = new ThreadLocal<>();
public LogAspect(LogService logService) {
this.logService = logService;
}
@@ -88,19 +82,18 @@ public class LogAspect {
//是否输出到日志文件
if (logInfo.isPrintToLogFile()) {
log.info("track_id:{},请求方法:{},请求方法参数:{}",trackId,methodName,params);
log.info("track_id:{},请求方法:{},请求方法参数:{}", trackId, methodName, params);
}
HttpServletRequest request = RequestHolder.getHttpServletRequest();
String requestIp = StringUtils.getIp(request);
Object result;
currentTime.set(System.currentTimeMillis());
long startTime = System.currentTimeMillis();
try {
result = joinPoint.proceed();
//是否把日志存到日志表
if (logInfo.isAddLogTable()) {
Log log = new Log("INFO", System.currentTimeMillis() - currentTime.get());
currentTime.remove();
logService.save(getUsername(), StringUtils.getBrowser(request), requestIp, joinPoint, log);
Log log = new Log("INFO", System.currentTimeMillis() - startTime);
logService.save("", StringUtils.getBrowser(request), requestIp, joinPoint, log);
}
if (logInfo.isInterfaceLog()) {
try {
@@ -113,25 +106,24 @@ public class LogAspect {
json.put("method", methodName);
json.put("params", getParameter(method, joinPoint.getArgs()));
json.put("request_ip", StringUtils.getIp(request));
// json.put("time", System.currentTimeMillis() - currentTime.get());
json.put("username", getUsername());
json.put("time", System.currentTimeMillis() - startTime);
json.put("username", "");
json.put("address", StringUtils.getCityInfo(requestIp));
json.put("browser", StringUtils.getBrowser(request));
json.put("exception_detail", IdUtil.getStringId());
json.put("create_time", DateUtil.now());
json.put("return_result", result.toString());
json.put("return_result", JSONObject.parse(result.toString()));
interfaceLog.insert(json);
} catch (Exception e) {
}
}
}catch (Exception ex){
log.error("track_id:{},error:{}",trackId,ex.getMessage());
Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
currentTime.remove();
} catch (Exception ex) {
log.error("track_id:{},error:{}", trackId, ex.getMessage());
Log log = new Log("ERROR", System.currentTimeMillis() - startTime);
log.setExceptionDetail(ThrowableUtil.getStackTrace(ex).getBytes());
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log);
throw ex;
logService.save("", StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log);
throw ex;
}
return result;
}
@@ -166,26 +158,11 @@ public class LogAspect {
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList);
}
/**
* 配置异常通知
*
* @param joinPoint join point for advice
* @param e exception
*/
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
Log log = new Log("ERROR",System.currentTimeMillis() - currentTime.get());
currentTime.remove();
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes());
HttpServletRequest request = RequestHolder.getHttpServletRequest();
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint)joinPoint, log);
}
public String getUsername() {
try {
return SecurityUtils.getCurrentUsername();
}catch (Exception e){
return "";
}
}
// public String getUsername() {
// try {
// return SecurityUtils.getCurrentUsername();
// } catch (Exception e) {
// return "";
// }
// }
}