Skip to content

Commit

Permalink
chore(request): per-request allocation reduction (#65)
Browse files Browse the repository at this point in the history
In the middleware handler: the very first init of `l zerolog.Logger` does not yet depend on the request's `c *gin.Context`. It need not be allocated at the start of every request, since all later `l` usage happens anyway only after a new instance is allocated by `l.With`.
  • Loading branch information
metaleap authored Nov 28, 2023
1 parent 31e8dd5 commit 4121d9d
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
}
}

l := zerolog.New(cfg.output).
Output(
zerolog.ConsoleWriter{
Out: cfg.output,
NoColor: !isTerm,
},
).
With().
Timestamp().
Logger()
return func(c *gin.Context) {
l := zerolog.New(cfg.output).
Output(
zerolog.ConsoleWriter{
Out: cfg.output,
NoColor: !isTerm,
},
).
With().
Timestamp().
Logger()

if cfg.logger != nil {
l = cfg.logger(c, l)
}
Expand Down

0 comments on commit 4121d9d

Please sign in to comment.