From 783f3ccf2af9ed0b47c38f65234ee47b9c903699 Mon Sep 17 00:00:00 2001 From: Alex Romanenko Date: Mon, 25 Oct 2021 12:37:54 -0500 Subject: [PATCH 1/3] fix #538: use 1 logger --- go.mod | 2 -- go.sum | 10 -------- logx/log.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ router/server.go | 37 ++++++---------------------- 4 files changed, 71 insertions(+), 41 deletions(-) diff --git a/go.mod b/go.mod index 9ed63a10c..9c913eccd 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/asdine/storm/v3 v3.2.1 github.com/buger/jsonparser v1.1.1 github.com/dgraph-io/badger/v3 v3.2103.1 - github.com/gin-contrib/logger v0.2.0 github.com/gin-gonic/gin v1.7.4 github.com/go-redis/redis/v8 v8.11.3 github.com/golang-queue/nats v0.0.2 @@ -24,7 +23,6 @@ require ( github.com/mitchellh/mapstructure v1.4.1 github.com/msalihkarakasli/go-hms-push v0.0.0-20210731212030-00e7b986815b github.com/prometheus/client_golang v1.10.0 - github.com/rs/zerolog v1.23.0 github.com/sideshow/apns2 v0.20.0 github.com/sirupsen/logrus v1.8.1 github.com/spf13/viper v1.7.1 diff --git a/go.sum b/go.sum index 8365407c6..42e31ce1e 100644 --- a/go.sum +++ b/go.sum @@ -81,7 +81,6 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= @@ -122,12 +121,9 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4 github.com/fukata/golang-stats-api-handler v1.0.0 h1:N6M25vhs1yAvwGBpFY6oBmMOZeJdcWnvA+wej8pKeko= github.com/fukata/golang-stats-api-handler v1.0.0/go.mod h1:1sIi4/rHq6s/ednWMZqTmRq3765qTUSs/c3xF6lj8J8= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/gin-contrib/logger v0.2.0 h1:YkdOGKdm/Nnrrd3bjBjcjd3ow1kR2KUfxxP4/rlL23E= -github.com/gin-contrib/logger v0.2.0/go.mod h1:dYxbt3GB+rvPyJSvox5lLsnKYwh8PjWrC9TQtR+hpUw= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= -github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM= github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -152,7 +148,6 @@ github.com/go-redis/redis/v8 v8.11.3/go.mod h1:xNJ9xDG09FsIPwh3bWdk+0oDWHbtF9rPN github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= -github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -424,9 +419,6 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= -github.com/rs/zerolog v1.23.0 h1:UskrK+saS9P9Y789yNNulYKdARjPZuS35B8gJF2x60g= -github.com/rs/zerolog v1.23.0/go.mod h1:6c7hFfxPOy7TacJc4Fcdi24/J0NKYGzjG8FWRI916Qo= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= @@ -644,7 +636,6 @@ golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -689,7 +680,6 @@ golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/logx/log.go b/logx/log.go index 056ac5b67..78042c95c 100644 --- a/logx/log.go +++ b/logx/log.go @@ -4,11 +4,14 @@ import ( "encoding/json" "errors" "fmt" + "net/http" "os" "strings" + "time" "github.com/appleboy/gorush/core" + "github.com/gin-gonic/gin" "github.com/mattn/go-isatty" "github.com/sirupsen/logrus" ) @@ -249,3 +252,63 @@ func LogPush(input *InputLog) LogPushEntry { return log } + +func GinLoggerMidleware(skipPath []string) gin.HandlerFunc { + + var skip map[string]struct{} + if length := len(skipPath); length > 0 { + skip = make(map[string]struct{}, length) + for _, path := range skipPath { + skip[path] = struct{}{} + } + } + + return func(c *gin.Context) { + start := time.Now().UTC() + + path := c.Request.URL.Path + raw := c.Request.URL.RawQuery + if raw != "" { + path = path + "?" + raw + } + + c.Next() + + track := true + + if _, ok := skip[path]; ok { + track = false + } + + if track { + + end := time.Now().UTC() + latency := end.Sub(start) + + msg := "Request" + if len(c.Errors) > 0 { + msg = c.Errors.String() + } + fields := logrus.Fields{ + "status": c.Writer.Status(), + "method": c.Request.Method, + "path": c.Request.URL.Path, + "ip": c.ClientIP(), + "latency": latency, + "user_agent": c.Request.UserAgent(), + } + switch { + case c.Writer.Status() >= http.StatusBadRequest && c.Writer.Status() < http.StatusInternalServerError: + { + LogError.WithContext(c).WithFields(fields).Warn(msg) + } + case c.Writer.Status() >= http.StatusInternalServerError: + { + LogError.WithContext(c).WithFields(fields).Error(msg) + } + default: + LogAccess.WithContext(c).WithFields(fields).Info(msg) + } + } + } +} diff --git a/router/server.go b/router/server.go index 7dd738e5f..1efceafdd 100644 --- a/router/server.go +++ b/router/server.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "net/http" - "os" "sync" "github.com/appleboy/gorush/config" @@ -17,15 +16,11 @@ import ( "github.com/appleboy/gorush/status" api "github.com/appleboy/gin-status-api" - "github.com/gin-contrib/logger" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/golang-queue/queue" - "github.com/mattn/go-isatty" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "github.com/thoas/stats" "golang.org/x/crypto/acme/autocert" ) @@ -165,23 +160,6 @@ func autoTLSServer(cfg *config.ConfYaml, q *queue.Queue) *http.Server { } func routerEngine(cfg *config.ConfYaml, q *queue.Queue) *gin.Engine { - zerolog.SetGlobalLevel(zerolog.InfoLevel) - if cfg.Core.Mode == "debug" { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } - - log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger() - - isTerm := isatty.IsTerminal(os.Stdout.Fd()) - if isTerm { - log.Logger = log.Output( - zerolog.ConsoleWriter{ - Out: os.Stdout, - NoColor: false, - }, - ) - } - // Support metrics doOnce.Do(func() { m := metric.NewMetrics(func() int { @@ -196,13 +174,14 @@ func routerEngine(cfg *config.ConfYaml, q *queue.Queue) *gin.Engine { r := gin.New() // Global middleware - r.Use(logger.SetLogger( - logger.WithUTC(true), - logger.WithSkipPath([]string{ - cfg.API.HealthURI, - cfg.API.MetricURI, - }), - )) + r.Use( + logx.GinLoggerMidleware( + []string{ + cfg.API.HealthURI, + cfg.API.MetricURI, + }, + ), + ) r.Use(gin.Recovery()) r.Use(VersionMiddleware()) r.Use(StatMiddleware()) From 822c65c7c4e96c4d124b02c996dbfd35283c9e66 Mon Sep 17 00:00:00 2001 From: Alex Romanenko Date: Mon, 25 Oct 2021 12:41:48 -0500 Subject: [PATCH 2/3] fix #538: clean code --- logx/log.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/logx/log.go b/logx/log.go index 78042c95c..d5246925e 100644 --- a/logx/log.go +++ b/logx/log.go @@ -299,13 +299,9 @@ func GinLoggerMidleware(skipPath []string) gin.HandlerFunc { } switch { case c.Writer.Status() >= http.StatusBadRequest && c.Writer.Status() < http.StatusInternalServerError: - { - LogError.WithContext(c).WithFields(fields).Warn(msg) - } + LogError.WithContext(c).WithFields(fields).Warn(msg) case c.Writer.Status() >= http.StatusInternalServerError: - { - LogError.WithContext(c).WithFields(fields).Error(msg) - } + LogError.WithContext(c).WithFields(fields).Error(msg) default: LogAccess.WithContext(c).WithFields(fields).Info(msg) } From 01526be453da2f3c3a0708dfd62c598400af14d3 Mon Sep 17 00:00:00 2001 From: Alex Romanenko Date: Mon, 25 Oct 2021 12:57:31 -0500 Subject: [PATCH 3/3] fix #538: fmt code --- logx/log.go | 1 - 1 file changed, 1 deletion(-) diff --git a/logx/log.go b/logx/log.go index d5246925e..d4ca86615 100644 --- a/logx/log.go +++ b/logx/log.go @@ -254,7 +254,6 @@ func LogPush(input *InputLog) LogPushEntry { } func GinLoggerMidleware(skipPath []string) gin.HandlerFunc { - var skip map[string]struct{} if length := len(skipPath); length > 0 { skip = make(map[string]struct{}, length)