diff --git a/README.md b/README.md index 32038fd3..71e431f3 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ | 项目类型 | GitCode | Gitee | Github | |--------|----------------------------------|-----------------------------------------------------------------------|------------------------------------------------------------------------| | 后端 | [youlai-boot](https://gitcode.com/youlai/youlai-boot) | [youlai-boot](https://gitee.com/youlaiorg/youlai-boot) | [youlai-boot](https://gitee.com/haoxianrui/youlai-boot) | -| 前端 | [vue3-element-admin](https://gitcode.com/youlai/vue3-element-admin) | [vue3-element-admin](https://gitee.com/youlai/vue3-element-admin) | [vue3-element-admin](https://github.com/youlaitech/vue3-element-admin) | -| 移动端 | [vue-uniapp-template](https://gitcode.com/youlai/vue-uniapp-template) | [vue-uniapp-template](https://gitcode.com/youlai/vue-uniapp-template) | [vue-uniapp-template](https://gitcode.com/youlaitech/vue-uniapp-template) | +| 前端 | [vue3-element-admin](https://gitcode.com/youlai/vue3-element-admin) | [vue3-element-admin](https://gitee.com/youlaiorg/vue3-element-admin) | [vue3-element-admin](https://github.com/youlaitech/vue3-element-admin) | +| 移动端 | [vue-uniapp-template](https://gitcode.com/youlai/vue-uniapp-template) | [vue-uniapp-template](https://gitee.com/youlaiorg/vue-uniapp-template) | [vue-uniapp-template](https://gitcode.com/youlaitech/vue-uniapp-template) | ## 📚 文档地址 diff --git a/src/main/java/com/youlai/boot/core/aspect/LogAspect.java b/src/main/java/com/youlai/boot/core/aspect/LogAspect.java index 6094bb56..d8f4bffe 100644 --- a/src/main/java/com/youlai/boot/core/aspect/LogAspect.java +++ b/src/main/java/com/youlai/boot/core/aspect/LogAspect.java @@ -19,10 +19,8 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.annotation.AfterReturning; -import org.aspectj.lang.annotation.AfterThrowing; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; import org.springframework.cache.CacheManager; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; @@ -61,23 +59,25 @@ public class LogAspect { * * @param joinPoint 切点 */ - @AfterReturning(pointcut = "logPointcut() && @annotation(logAnnotation)", returning = "jsonResult") - public void doAfterReturning(JoinPoint joinPoint, com.youlai.boot.common.annotation.Log logAnnotation, Object jsonResult) { - this.saveLog(joinPoint, null, jsonResult, logAnnotation); + @Around("logPointcut() && @annotation(logAnnotation)") + public Object doAround(ProceedingJoinPoint joinPoint, com.youlai.boot.common.annotation.Log logAnnotation) throws Throwable { + TimeInterval timer = DateUtil.timer(); + Object result = null; + Exception exception = null; + + try { + result = joinPoint.proceed(); + } catch (Exception e) { + exception = e; + throw e; + } finally { + long executionTime = timer.interval(); // 执行时长 + this.saveLog(joinPoint, exception, result, logAnnotation, executionTime); + } + return result; } - /** - * 拦截异常操作 - * - * @param joinPoint 切点 - * @param e 异常 - */ - @AfterThrowing(value = "logPointcut()", throwing = "e") - public void doAfterThrowing(JoinPoint joinPoint, Exception e) { - this.saveLog(joinPoint, e, null, null); - } - /** * 保存日志 * @@ -86,15 +86,11 @@ public class LogAspect { * @param jsonResult 响应结果 * @param logAnnotation 日志注解 */ - private void saveLog(final JoinPoint joinPoint, final Exception e, Object jsonResult, com.youlai.boot.common.annotation.Log logAnnotation) { + private void saveLog(final JoinPoint joinPoint, final Exception e, Object jsonResult, com.youlai.boot.common.annotation.Log logAnnotation, long executionTime) { String requestURI = request.getRequestURI(); - - TimeInterval timer = DateUtil.timer(); - // 执行方法 - long executionTime = timer.interval(); - // 创建日志记录 Log log = new Log(); + log.setExecutionTime(executionTime); if (logAnnotation == null && e != null) { log.setModule(LogModuleEnum.EXCEPTION); log.setContent("系统发生异常"); @@ -129,7 +125,7 @@ public class LogAspect { } } - log.setExecutionTime(executionTime); + // 获取浏览器和终端系统信息 String userAgentString = request.getHeader("User-Agent"); UserAgent userAgent = resolveUserAgent(userAgentString);