From 81d89ee454264ea92b97bc87713217c36da00368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Efe=20=C3=87etin?= Date: Tue, 6 Sep 2022 15:04:10 +0300 Subject: [PATCH] v3: fix logger benchmarks --- middleware/logger/README.md | 4 ++-- middleware/logger/config.go | 2 +- middleware/logger/default_logger.go | 5 ++--- middleware/logger/logger.go | 22 +++++++++++++--------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/middleware/logger/README.md b/middleware/logger/README.md index c6b99fb645..c77d4f6afb 100644 --- a/middleware/logger/README.md +++ b/middleware/logger/README.md @@ -95,7 +95,7 @@ func main() { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) - app.Use(logger.New(logger.Config{LoggerFunc: func(c fiber.Ctx, data logger.LoggerData, cfg logger.Config) error { + app.Use(logger.New(logger.Config{LoggerFunc: func(c fiber.Ctx, data *logger.LoggerData, cfg logger.Config) error { log.Info(). Str("path", c.Path()). Str("method", c.Method()). @@ -158,7 +158,7 @@ type Config struct { // If you don't define anything for this field, it'll use classical logger of Fiber. // // Optional. Default: defaultLogger - LoggerFunc func(c fiber.Ctx, data LoggerData, cfg Config) error + LoggerFunc func(c fiber.Ctx, data *LoggerData, cfg Config) error } ``` diff --git a/middleware/logger/config.go b/middleware/logger/config.go index 80dd3288c9..700f2c6942 100644 --- a/middleware/logger/config.go +++ b/middleware/logger/config.go @@ -51,7 +51,7 @@ type Config struct { // If you don't define anything for this field, it'll use default logger of Fiber. // // Optional. Default: defaultLogger - LoggerFunc func(c fiber.Ctx, data LoggerData, cfg Config) error + LoggerFunc func(c fiber.Ctx, data *LoggerData, cfg Config) error enableColors bool enableLatency bool diff --git a/middleware/logger/default_logger.go b/middleware/logger/default_logger.go index 822cb676d6..b85aa61702 100644 --- a/middleware/logger/default_logger.go +++ b/middleware/logger/default_logger.go @@ -28,11 +28,10 @@ type LoggerData struct { } var tmpl *fasttemplate.Template +var mu sync.Mutex // default logger for fiber -func defaultLogger(c fiber.Ctx, data LoggerData, cfg Config) error { - var mu sync.Mutex - +func defaultLogger(c fiber.Ctx, data *LoggerData, cfg Config) error { // Alias colors colors := c.App().Config().ColorScheme diff --git a/middleware/logger/logger.go b/middleware/logger/logger.go index 0baa0424b9..4a25271f7a 100644 --- a/middleware/logger/logger.go +++ b/middleware/logger/logger.go @@ -99,6 +99,13 @@ func New(config ...Config) fiber.Handler { // Before handling func cfg.BeforeHandlerFunc(cfg) + // Logger data + data := &LoggerData{ + Pid: pid, + ErrPaddingStr: errPaddingStr, + Timestamp: timestamp, + } + // Return new handler return func(c fiber.Ctx) (err error) { // Don't execute middleware if Next returns true @@ -144,15 +151,12 @@ func New(config ...Config) fiber.Handler { stop = time.Now() } - // Logger instance - if err = cfg.LoggerFunc(c, LoggerData{ - Pid: pid, - ErrPaddingStr: errPaddingStr, - ChainErr: chainErr, - Start: start, - Stop: stop, - Timestamp: timestamp, - }, cfg); err != nil { + // Logger instance & update some logger data fields + data.ChainErr = chainErr + data.Start = start + data.Stop = stop + + if err = cfg.LoggerFunc(c, data, cfg); err != nil { return err }