From 10934b9dfe1d7905234c59b261aff60f3b8f04e2 Mon Sep 17 00:00:00 2001 From: zhangzhiqiang Date: Sun, 5 Feb 2023 19:25:18 +0800 Subject: [PATCH] add:log --- .../nl/modules/logging/aspect/LogAspect.java | 91 +++++++++---------- .../src/main/resources/logback-spring.xml | 22 +++++ 2 files changed, 65 insertions(+), 48 deletions(-) diff --git a/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/LogAspect.java b/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/LogAspect.java index 18b788df7..8883ba120 100644 --- a/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/LogAspect.java +++ b/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/LogAspect.java @@ -42,10 +42,7 @@ 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.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @author Zheng Jie @@ -79,6 +76,7 @@ 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(); // 方法路径 @@ -89,54 +87,51 @@ public class LogAspect { //是否输出到日志文件 if (logInfo.isPrintToLogFile()) { - log.info("请求方法:{}", methodName); - log.info("请求方法参数:{}", params); + log.info("track_id:{},请求方法:{},请求方法参数:{}",trackId,methodName,params); } - - + HttpServletRequest request = RequestHolder.getHttpServletRequest(); + String requestIp = StringUtils.getIp(request); Object result; currentTime.set(System.currentTimeMillis()); - result = joinPoint.proceed(); - - HttpServletRequest request = RequestHolder.getHttpServletRequest(); - - String requestIp = StringUtils.getIp(request); - //是否把日志存到日志表 - 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 { - 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.fromObject(result).getJSONObject("body")); - interfaceLog.insert(json); - } catch (Exception e) { - System.out.println("接口日志保存异常!"+e.getMessage()); + 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) { + } + } + }catch (Exception ex){ + log.error("track_id:{},error:{}",trackId,ex.getMessage()); + Log log = new Log("ERROR", 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; + } return result; } @@ -176,7 +171,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(); diff --git a/lms/nladmin-system/src/main/resources/logback-spring.xml b/lms/nladmin-system/src/main/resources/logback-spring.xml index ef9846ea6..0ded305e8 100644 --- a/lms/nladmin-system/src/main/resources/logback-spring.xml +++ b/lms/nladmin-system/src/main/resources/logback-spring.xml @@ -56,6 +56,26 @@ https://juejin.cn/post/6844903775631572999 + + ${LOG_HOME}/%d{yyyy-MM-dd}.%i.error.log + + error + + + ${LOG_HOME}/%d{yyyy-MM-dd}.%i.error.log + true + 15 + 300MB + + 3 + + + + ${log.pattern} + ${log.charset} + + + 0 @@ -78,6 +98,7 @@ https://juejin.cn/post/6844903775631572999 + @@ -124,6 +145,7 @@ https://juejin.cn/post/6844903775631572999 +