From 1c666637cda2b170221bb6f98cba44d01b416f0c Mon Sep 17 00:00:00 2001 From: tiglog Date: Tue, 17 Oct 2023 18:03:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=20reqId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gweb/ginlog/access_log.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gweb/ginlog/access_log.go b/gweb/ginlog/access_log.go index 4d5781c..04fac21 100644 --- a/gweb/ginlog/access_log.go +++ b/gweb/ginlog/access_log.go @@ -32,6 +32,9 @@ func GinLogger(logfile string) gin.HandlerFunc { if latency > time.Minute { latency = latency.Truncate(time.Second) } + if requestID := c.Writer.Header().Get("X-Request-Id"); requestID != "" { + logger.With(logger.String("reqId", requestID)) + } fields := []logger.Field{ logger.String("start", start.Format(time.RFC3339)), logger.Int("status", c.Writer.Status()), @@ -43,9 +46,6 @@ func GinLogger(logfile string) gin.HandlerFunc { logger.String("userAgent", c.Request.UserAgent()), logger.String("error", c.Errors.ByType(gin.ErrorTypePrivate).String()), } - if requestID := c.Writer.Header().Get("X-Request-Id"); requestID != "" { - fields = append(fields, logger.String("request_id", requestID)) - } log.Info("GIN request", fields...) }