From 2a70a9a252df2d37480933de54f538baf44396fc Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 21 Dec 2023 20:57:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(log/httptrace-pro):=20=E4=BF=AE=E5=A4=8D=20?= =?UTF-8?q?IP=20=E9=85=8D=E7=BD=AE=E8=A7=A3=E6=9E=90=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E7=BC=A9=E5=B0=8F=E6=97=A5=E5=BF=97=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=BD=B1=E5=93=8D=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/ProjectProperties.java | 4 +-- .../continew/starter/core/util/IpUtils.java | 9 +++++- .../httptracepro/handler/LogInterceptor.java | 28 +++++++++++-------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectProperties.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectProperties.java index 3e0ba824..c8025e88 100644 --- a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectProperties.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectProperties.java @@ -82,8 +82,8 @@ public class ProjectProperties { public static final boolean IP_ADDR_LOCAL_PARSE_ENABLED; static { - String underlineCaseProperty = SpringUtil.getProperty("ip-addr-local-parse-enabled"); - String camelCaseProperty = SpringUtil.getProperty("ipAddrLocalParseEnabled"); + String underlineCaseProperty = SpringUtil.getProperty("project.ip-addr-local-parse-enabled"); + String camelCaseProperty = SpringUtil.getProperty("project.ipAddrLocalParseEnabled"); IP_ADDR_LOCAL_PARSE_ENABLED = Convert.toBool(underlineCaseProperty, false) || Convert.toBool(camelCaseProperty, false); } diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/IpUtils.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/IpUtils.java index 2b30a200..b1a08b80 100644 --- a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/IpUtils.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/IpUtils.java @@ -16,7 +16,9 @@ package top.charles7c.continew.starter.core.util; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.net.NetUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; import cn.hutool.http.HtmlUtil; import cn.hutool.http.HttpUtil; @@ -28,6 +30,9 @@ import lombok.extern.slf4j.Slf4j; import net.dreamlu.mica.ip2region.core.Ip2regionSearcher; import net.dreamlu.mica.ip2region.core.IpInfo; import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties; +import top.charles7c.continew.starter.core.constant.StringConstants; + +import java.util.Set; /** * IP 工具类 @@ -86,7 +91,9 @@ public class IpUtils { Ip2regionSearcher ip2regionSearcher = SpringUtil.getBean(Ip2regionSearcher.class); IpInfo ipInfo = ip2regionSearcher.memorySearch(ip); if (null != ipInfo) { - return ipInfo.getAddress(); + Set regionSet = CollUtil.newLinkedHashSet(ipInfo.getAddress(), ipInfo.getIsp()); + regionSet.removeIf(StrUtil::isBlank); + return String.join(StringConstants.SPACE, regionSet); } return null; } diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java index a6f6e421..69b6b924 100644 --- a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java +++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java @@ -69,19 +69,23 @@ public class LogInterceptor implements HandlerInterceptor { timestampTtl.remove(); Set includeSet = properties.getInclude(); RecordableServletHttpRequest sourceRequest = new RecordableServletHttpRequest(request); - LogRecord.Started startedLogRecord = LogRecord.start(timestamp, sourceRequest); - RecordableServletHttpResponse sourceResponse = new RecordableServletHttpResponse(response, response.getStatus()); - LogRecord finishedLogRecord = startedLogRecord.finish(sourceResponse, includeSet); - HandlerMethod handlerMethod = (HandlerMethod) handler; - if (includeSet.contains(Include.DESCRIPTION)) { - // 记录日志描述 - this.logDescription(finishedLogRecord, handlerMethod); + try { + LogRecord.Started startedLogRecord = LogRecord.start(timestamp, sourceRequest); + RecordableServletHttpResponse sourceResponse = new RecordableServletHttpResponse(response, response.getStatus()); + LogRecord finishedLogRecord = startedLogRecord.finish(sourceResponse, includeSet); + HandlerMethod handlerMethod = (HandlerMethod) handler; + if (includeSet.contains(Include.DESCRIPTION)) { + // 记录日志描述 + this.logDescription(finishedLogRecord, handlerMethod); + } + if (includeSet.contains(Include.MODULE)) { + // 记录所属模块 + this.logModule(finishedLogRecord, handlerMethod); + } + dao.add(finishedLogRecord); + } catch (Exception ex) { + log.error("Logging http log occurred an error: {}.", ex.getMessage(), ex); } - if (includeSet.contains(Include.MODULE)) { - // 记录所属模块 - this.logModule(finishedLogRecord, handlerMethod); - } - dao.add(finishedLogRecord); } /**