From f9850c326458f057c36d3a36d19e1c97901dc856 Mon Sep 17 00:00:00 2001 From: zegkljan Date: Tue, 27 Sep 2022 21:56:28 +0200 Subject: [PATCH] Debug logging configuration. * Accepts a -debug option which sets logging level to debug. --- main.go | 2 ++ server/handling.go | 3 +-- server/server.go | 5 ++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 7d3ac13..e17bd2a 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ func main() { maxPhotoXArg := flag.Int("max-photo-width", 0, "Maximum width of photos. 0 means no limit.") maxPhotoYArg := flag.Int("max-photo-height", 0, "Maximum height of photos. 0 means no limit.") photoQualityArg := flag.Int("photo-quality", 90, "Photo JPEG quality.") + debug := flag.Bool("debug", false, "If specified, logging level will be set to debug instead of info.") flag.Parse() @@ -66,6 +67,7 @@ func main() { MaxPhotoX: *maxPhotoXArg, MaxPhotoY: *maxPhotoYArg, PhotoQuality: *photoQualityArg, + Debug: *debug, }) sigs := make(chan os.Signal, 1) diff --git a/server/handling.go b/server/handling.go index 2cf160e..eba61dc 100644 --- a/server/handling.go +++ b/server/handling.go @@ -35,7 +35,6 @@ func (s *Server) setupRouter() *gin.Engine { router.Use(gin.Recovery()) // logging - ginLogger := logrus.New() hostname, err := os.Hostname() if err != nil { hostname = "unknown" @@ -54,7 +53,7 @@ func (s *Server) setupRouter() *gin.Engine { if dataLength < 0 { dataLength = 0 } - entry := ginLogger.WithFields(logrus.Fields{ + entry := s.log.WithFields(logrus.Fields{ "hostname": hostname, "statusCode": statusCode, "latency": latency, diff --git a/server/server.go b/server/server.go index a0766d3..7534157 100644 --- a/server/server.go +++ b/server/server.go @@ -53,6 +53,7 @@ type ServerConfig struct { MaxPhotoX int MaxPhotoY int PhotoQuality int + Debug bool } func New(config ServerConfig) *Server { @@ -63,7 +64,9 @@ func New(config ServerConfig) *Server { func (s *Server) Run(ctx context.Context) { s.log = logrus.New() - s.log.SetLevel(logrus.DebugLevel) + if s.config.Debug { + s.log.SetLevel(logrus.DebugLevel) + } s.ctx = ctx s.setupDB()