From f552fbd9cf9f60d642598189fd6e44efa8ea6ecb Mon Sep 17 00:00:00 2001 From: duxiaobo Date: Mon, 28 Oct 2019 16:45:23 +0800 Subject: [PATCH] feat: gin access log add exclude path --- go.mod | 2 +- go.sum | 4 ++++ xgin/access_logger.go | 12 +++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 375488d1..c5baece1 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/go-playground/universal-translator v0.16.0 // indirect github.com/gomodule/redigo v2.0.0+incompatible github.com/leodido/go-urn v1.1.0 // indirect - github.com/nickxb/gin-http-logger v0.0.0-20190523055149-762fe88a7fa4 + github.com/nickxb/gin-http-logger v1.1.1-0.20191028062356-c63c682255f4 github.com/pkg/errors v0.8.1 github.com/sirupsen/logrus v1.4.2 gopkg.in/go-playground/validator.v9 v9.28.0 diff --git a/go.sum b/go.sum index 1c0de8f0..cb8ee154 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,10 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/nickxb/gin-http-logger v0.0.0-20190523055149-762fe88a7fa4 h1:nXwOIGLh6rxIxtmEUX8+NSd5JKtTWHz9NoD8qOhFPMk= github.com/nickxb/gin-http-logger v0.0.0-20190523055149-762fe88a7fa4/go.mod h1:x9H4fWPQ7atgzXuYGWh3MT/JoWP05Znnz9SgTwxeArU= +github.com/nickxb/gin-http-logger v1.1.1-0.20191028062356-c63c682255f4 h1:fk4PMF0STJnoCrwBAUIScOhJRfgmxpaPcWtEoAzSL7Y= +github.com/nickxb/gin-http-logger v1.1.1-0.20191028062356-c63c682255f4/go.mod h1:5C1/EWTLeUabG+97z49M9MbFJE0LP07a9VlfSqlyk/4= +github.com/nickxb/gin-http-logger v2.0.2+incompatible h1:MXlWZnzi2vjT+/iiKHfAuXoj2rZ/IVcm8bKjaTA3iiE= +github.com/nickxb/gin-http-logger v2.0.2+incompatible/go.mod h1:U/v8B2W5hUoARNdceiMKs6YG3zsI1VxuA9uH4B33/7I= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/xgin/access_logger.go b/xgin/access_logger.go index cd636224..93e853cf 100644 --- a/xgin/access_logger.go +++ b/xgin/access_logger.go @@ -2,13 +2,14 @@ package xgin import ( "fmt" - httpLogger "github.com/nickxb/gin-http-logger" + "os" + "github.com/gin-gonic/gin" + httpLogger "github.com/nickxb/gin-http-logger" "github.com/sirupsen/logrus" - "os" ) -func AccessLogger(file string, level string) gin.HandlerFunc { +func AccessLogger(file string, level string, excludePaths ...string) gin.HandlerFunc { accessLogger := logrus.New() accessLogger.SetFormatter(&logrus.TextFormatter{ TimestampFormat: "2006-01-02 15:04:05.000", @@ -33,5 +34,10 @@ func AccessLogger(file string, level string) gin.HandlerFunc { DropSize: 1024 * 10, //10k } + alc.ExcludePaths = map[string]bool{} + for _, excludePath := range excludePaths { + alc.ExcludePaths[excludePath] = true + } + return httpLogger.New(alc) }