Skip to content

Commit

Permalink
v3: fix logger benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Sep 6, 2022
1 parent 90c1c4c commit 81d89ee
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
4 changes: 2 additions & 2 deletions middleware/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()).
Expand Down Expand Up @@ -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
}
```

Expand Down
2 changes: 1 addition & 1 deletion middleware/logger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions middleware/logger/default_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 13 additions & 9 deletions middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

1 comment on commit 81d89ee

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 81d89ee Previous: f1986b9 Ratio
Benchmark_AcquireCtx 1916 ns/op 1568 B/op 5 allocs/op 678.2 ns/op 1568 B/op 5 allocs/op 2.83

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.