From 474380bd97a17516a850b38bb3c2ffab835612c0 Mon Sep 17 00:00:00 2001 From: carolcoral <947752894@qq.com> Date: Mon, 13 Dec 2021 22:39:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96LoggerUtil=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cnkj/common/utils/logger/LoggerUtil.java | 86 +++++++++++++++---- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/src/main/java/site/cnkj/common/utils/logger/LoggerUtil.java b/src/main/java/site/cnkj/common/utils/logger/LoggerUtil.java index a6d5fde..311740b 100644 --- a/src/main/java/site/cnkj/common/utils/logger/LoggerUtil.java +++ b/src/main/java/site/cnkj/common/utils/logger/LoggerUtil.java @@ -3,14 +3,12 @@ package site.cnkj.common.utils.logger; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.Data; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.springframework.lang.Nullable; import site.cnkj.common.utils.bean.ReflectionUtil; -import site.cnkj.common.utils.data.GlobalId; import site.cnkj.common.utils.date.DateUtil; import java.io.PrintWriter; @@ -20,14 +18,17 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Date; import java.util.Map; +import java.util.UUID; -/* - * @version 1.0 created by LXW on 2019/4/1 9:13 - */ +/** + * 日志打印 + * + * @author Liu XueWen, 2021-12-06 + * @version EasyUtil v0.3.0 + **/ +@Slf4j public class LoggerUtil { - private static final Logger log = LoggerFactory.getLogger(LoggerUtil.class); - enum Level{ INFO("INFO"), WARN("WARN"), @@ -71,7 +72,7 @@ public class LoggerUtil { private String traceId = getMDC(); private String requestId = MDC.get("requestId"); private String hostName = HOST_NAME;//机器名 - private String time = DateUtil.translateDateToString(new Date(), DateUtil.FORMAT.FULL_TIME_YMD_HMS_S.getValue());//时间 + private String time = DateUtil.translateDateToString(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");//时间 private Long timestamp = new Date().getTime(); private String className = new Throwable().getStackTrace()[4].getClassName(); private String methodName = new Throwable().getStackTrace()[4].getMethodName(); @@ -89,7 +90,7 @@ public class LoggerUtil { static String getMDC(){ if (StringUtils.isEmpty(MDC.get(TRACE_ID))){ - String traceID = "TraceID: " + GlobalId.generateNextId(); + String traceID = "TraceID: " + UUID.randomUUID().toString(); MDC.put(TRACE_ID, traceID); } return MDC.get(TRACE_ID).split(":")[1].trim(); @@ -160,9 +161,7 @@ public class LoggerUtil { loggerEntity.setInfo(message); loggerEntity.setStackTrace(throwable.getMessage()); JSONObject jsonSource = JSON.parseObject(loggerEntity.toString()); - for (Map.Entry entry : fields.entrySet()) { - jsonSource.put(entry.getKey(), entry.getValue()); - } + jsonSource.putAll(fields); logger(level, JSON.toJSONString(jsonSource)); } @@ -173,12 +172,28 @@ public class LoggerUtil { loggerEntity.setInfo(message); loggerEntity.setStackTrace(formatException(e)); JSONObject jsonSource = JSON.parseObject(loggerEntity.toString()); - for (Map.Entry entry : fields.entrySet()) { - jsonSource.put(entry.getKey(), entry.getValue()); - } + jsonSource.putAll(fields); logger(level, JSON.toJSONString(jsonSource)); } + private static String formatInputString(String message, Object... args){ + StringBuilder stringBuilder = new StringBuilder(); + String[] messages = message.split("\\{\\}", -1); + for (int i = 0; i < messages.length; i++) { + stringBuilder.append(messages[i]); + if (i < args.length){ + Object arg = args[i]; + stringBuilder.append(String.valueOf(arg)); + } + } + return stringBuilder.toString(); + } + + public static void info(String message, Object... args){ + String inputString = formatInputString(message, args); + message(Level.INFO, inputString); + } + public static void info(String message){ message(Level.INFO, message); } @@ -207,6 +222,11 @@ public class LoggerUtil { message(Level.WARN, message); } + public static void warn(String message, Object... args){ + String inputString = formatInputString(message, args); + message(Level.WARN, inputString); + } + public static void warn(String message, Map fields){ fields.remove("data"); map(Level.WARN, message, fields); @@ -228,10 +248,44 @@ public class LoggerUtil { mapException(Level.WARN, message, fields, e); } + public static void debug(String message){ + message(Level.DEBUG, message); + } + + public static void debug(String message, Object... args){ + String inputString = formatInputString(message, args); + message(Level.DEBUG, inputString); + } + + public static void debug(String message, Map fields){ + map(Level.DEBUG, message, fields); + } + + public static void debug(String message, Throwable throwable){ + throwable(Level.DEBUG, message, throwable); + } + + public static void debug(String message, Exception e){ + exception(Level.DEBUG, message, e); + } + + public static void debug(String message, Map fields, Throwable throwable){ + mapThrowable(Level.DEBUG, message, fields, throwable); + } + + public static void debug(String message, Map fields, Exception e){ + mapException(Level.DEBUG, message, fields, e); + } + public static void error(String message){ message(Level.ERROR, message); } + public static void error(String message, Object... args){ + String inputString = formatInputString(message, args); + message(Level.ERROR, inputString); + } + public static void error(String message, Map fields){ map(Level.ERROR, message, fields); }