From d14c647dc9b60deda9977c0583981bbd3c154920 Mon Sep 17 00:00:00 2001 From: Granty1 Date: Mon, 30 Dec 2019 19:37:03 +0800 Subject: [PATCH] Delete log in reportformat method --- .../controller/servers/reportformat.go | 20 ++------------ QMPlusServer/middleware/logger.go | 27 ++++++++++++------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/QMPlusServer/controller/servers/reportformat.go b/QMPlusServer/controller/servers/reportformat.go index 6315b9e0..5e6f25cb 100644 --- a/QMPlusServer/controller/servers/reportformat.go +++ b/QMPlusServer/controller/servers/reportformat.go @@ -1,29 +1,13 @@ package servers import ( - "gin-vue-admin/init/qmlog" - "github.com/gin-gonic/gin" "net/http" - "time" + + "github.com/gin-gonic/gin" ) func ReportFormat(c *gin.Context, success bool, msg string, json gin.H) { // 开始时间 - start := time.Now() - path := c.Request.URL.Path - clientIP := c.ClientIP() - method := c.Request.Method - statusCode := c.Writer.Status() - qmlog.QMLog.Infof("| %3d | %13v | %15s | %s %s |%s|", - statusCode, - start, - clientIP, - method, path, gin.H{ - "success": success, - "msg": msg, - "data": json, - }, - ) c.JSON(http.StatusOK, gin.H{ "success": success, "msg": msg, diff --git a/QMPlusServer/middleware/logger.go b/QMPlusServer/middleware/logger.go index 508ec472..0ec4bb6c 100644 --- a/QMPlusServer/middleware/logger.go +++ b/QMPlusServer/middleware/logger.go @@ -2,27 +2,35 @@ package middleware import ( "bytes" + "gin-vue-admin/init/qmlog" "net/http/httputil" + "strings" "time" "github.com/gin-gonic/gin" - "qiniupkg.com/x/log.v7" ) func Logger() gin.HandlerFunc { + log := qmlog.QMLog return func(c *gin.Context) { // request time start := time.Now() // request path path := c.Request.URL.Path + logFlag := true + if strings.Contains(path, "swagger") { + logFlag = false + } // request ip clientIP := c.ClientIP() // method method := c.Request.Method // copy request content req, _ := httputil.DumpRequest(c.Request, true) - log.Infof(`| %s | %s | %s | %5s | %s\n`, - `Request :`, method, clientIP, path, string(req)) + if logFlag { + log.Infof(`| %s | %s | %s | %5s | %s\n`, + `Request :`, method, clientIP, path, string(req)) + } // replace writer cusWriter := &responseBodyWriter{ ResponseWriter: c.Writer, @@ -36,12 +44,13 @@ func Logger() gin.HandlerFunc { //execute time latency := end.Sub(start) statusCode := c.Writer.Status() - - log.Infof(`| %s | %3d | %13v | %s \n`, - `Response:`, - statusCode, - latency, - cusWriter.body.String()) + if logFlag { + log.Infof(`| %s | %3d | %13v | %s \n`, + `Response:`, + statusCode, + latency, + cusWriter.body.String()) + } } }