From e28e0ca9a2be041d8d38b93692e1a20eae9ac81c Mon Sep 17 00:00:00 2001 From: smallchill Date: Wed, 27 Feb 2019 23:05:45 +0800 Subject: [PATCH] =?UTF-8?q?:tada:=20=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/boot/logger/RequestLogAspect.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/blade-core-boot/src/main/java/org/springblade/core/boot/logger/RequestLogAspect.java b/blade-core-boot/src/main/java/org/springblade/core/boot/logger/RequestLogAspect.java index 9cc1254..2822d2f 100644 --- a/blade-core-boot/src/main/java/org/springblade/core/boot/logger/RequestLogAspect.java +++ b/blade-core-boot/src/main/java/org/springblade/core/boot/logger/RequestLogAspect.java @@ -101,30 +101,47 @@ public class RequestLogAspect { } }); needRemoveKeys.forEach(paraMap::remove); + + // 构建成一条长 日志,避免并发下日志错乱 + StringBuilder logBuilder = new StringBuilder(500); + // 日志参数 + List logArgs = new ArrayList<>(); + logBuilder.append("\n\n================ Request Start ================\n"); // 打印请求 - log.info("================ Request Start ================"); if (paraMap.isEmpty()) { - log.info("===> {}: {}", requestMethod, requestURI); + logBuilder.append("===> {}: {}\n"); + logArgs.add(requestMethod); + logArgs.add(requestURI); } else { - log.info("===> {}: {} Parameters: {}", requestMethod, requestURI, JsonUtil.toJson(paraMap)); + logBuilder.append("===> {}: {} Parameters: {}\n"); + logArgs.add(requestMethod); + logArgs.add(requestURI); + logArgs.add(JsonUtil.toJson(paraMap)); } // 打印请求头 Enumeration headers = request.getHeaderNames(); while (headers.hasMoreElements()) { String headerName = headers.nextElement(); String headerValue = request.getHeader(headerName); - log.info("===headers=== {} : {}", headerName, headerValue); + logBuilder.append("===headers=== {} : {}\n"); + logArgs.add(headerName); + logArgs.add(headerValue); } // 打印执行时间 long startNs = System.nanoTime(); try { Object result = point.proceed(); - log.info("===Result=== {}", JsonUtil.toJson(result)); + logBuilder.append("===Result=== {}\n"); + logArgs.add(JsonUtil.toJson(result)); return result; } finally { long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); - log.info("<=== {}: {} ({} ms)", request.getMethod(), requestURI, tookMs); - log.info("================ Request End ================"); + logBuilder.append("<=== {}: {} ({} ms)"); + logArgs.add(requestMethod); + logArgs.add(requestURI); + logArgs.add(tookMs); + logBuilder.append("\n================ Request End ================\n"); + log.info(logBuilder.toString(), logArgs.toArray()); } }