去掉plumelog

This commit is contained in:
ludj
2023-02-05 19:31:38 +08:00
parent 7bf3ccc8c8
commit 5ce34f4a29
7 changed files with 84 additions and 198 deletions

View File

@@ -18,8 +18,8 @@ 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.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
@@ -42,7 +42,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Zheng Jie
@@ -76,7 +79,6 @@ public class LogAspect {
*/
@Around("logPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
String trackId = UUID.randomUUID().toString();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
// 方法路径
@@ -87,51 +89,54 @@ public class LogAspect {
//是否输出到日志文件
if (logInfo.isPrintToLogFile()) {
log.info("track_id:{},请求方法:{},请求方法参数:{}",trackId,methodName,params);
log.info("请求方法:{}", methodName);
log.info("请求方法参数:{}", params);
}
HttpServletRequest request = RequestHolder.getHttpServletRequest();
String requestIp = StringUtils.getIp(request);
Object result;
currentTime.set(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);
}
if (logInfo.isInterfaceLog()) {
try {
WQLObject interfaceLog = WQLObject.getWQLObject("sys_interface_log");
JSONObject json = new JSONObject();
json.put("log_id", IdUtil.getStringId());
json.put("description", logInfo.value());
json.put("log_type", logInfo.interfaceLogType().getDesc());
json.put("log_level", "1");
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("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", JSONObject.fromObject(result).getJSONObject("body"));
interfaceLog.insert(json);
} catch (Exception e) {
result = joinPoint.proceed();
}
}
}catch (Exception ex){
log.error("track_id:{},error:{}",trackId,ex.getMessage());
Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
HttpServletRequest request = RequestHolder.getHttpServletRequest();
String requestIp = StringUtils.getIp(request);
//是否把日志存到日志表
if (logInfo.isAddLogTable()) {
Log log = new Log("INFO", System.currentTimeMillis() - currentTime.get());
currentTime.remove();
log.setExceptionDetail(ThrowableUtil.getStackTrace(ex).getBytes());
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log);
throw ex;
logService.save(getUsername(), StringUtils.getBrowser(request), requestIp, joinPoint, log);
}
//保存到接口日志表
if (logInfo.isInterfaceLog()) {
try {
System.out.println("日志存储到接口日志表!");
WQLObject interfaceLog = WQLObject.getWQLObject("sys_interface_log");
JSONObject json = new JSONObject();
json.put("log_id", IdUtil.getStringId());
json.put("description", logInfo.value());
json.put("log_type", logInfo.interfaceLogType().getDesc());
json.put("log_level", "1");
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("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", JSONObject.parse(result.toString()));
interfaceLog.insert(json);
} catch (Exception e) {
System.out.println("接口日志保存异常!"+e.getMessage());
}
}
return result;
}
@@ -171,7 +176,7 @@ public class LogAspect {
* @param joinPoint join point for advice
* @param e exception
*/
// @AfterThrowing(pointcut = "logPointcut()", throwing = "e")
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
Log log = new Log("ERROR", System.currentTimeMillis() - currentTime.get());
currentTime.remove();