From 5e52bdcfa834e40df04885a454432bf21535a927 Mon Sep 17 00:00:00 2001 From: Night_mare <55904958+chong-chonga@users.noreply.github.com> Date: Thu, 2 Dec 2021 20:04:15 +0800 Subject: [PATCH] Update LogServiceImpl.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 断言不适合用于参数验证,因为断言可以在JVM中的运行时被禁用,这意味着错误的操作设置将完全消除预期的检查。此外,失败的断言会抛出断言错误,而不是抛出某种类型的异常。抛出错误完全超出了正常程序中预期的捕获/抛出行为的正常范围。使用 if 代替 断言, 并抛出 IllegalArgumentException 更可控 --- .../java/me/zhengjie/service/impl/LogServiceImpl.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java b/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java index 801078f1..b86cc89e 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java @@ -79,7 +79,9 @@ public class LogServiceImpl implements LogService { @Override @Transactional(rollbackFor = Exception.class) public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log) { - + if (log == null) { + throw new IllegalArgumentException("Log 不能为 null!"); + } MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); me.zhengjie.annotation.Log aopLog = method.getAnnotation(me.zhengjie.annotation.Log.class); @@ -88,12 +90,9 @@ public class LogServiceImpl implements LogService { String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; // 描述 - if (log != null) { - log.setDescription(aopLog.value()); - } - assert log != null; + log.setDescription(aopLog.value()); + log.setRequestIp(ip); - log.setAddress(StringUtils.getCityInfo(log.getRequestIp())); log.setMethod(methodName); log.setUsername(username);