Skip to content

Commit

Permalink
Remove call to isatty from the hot path.
Browse files Browse the repository at this point in the history
The result from isatty is immutable during the run of the program and
is triggering a heavy system call that significantly slow down answering
request.

$ benchstat before.txt after.txt
goos: darwin
goarch: amd64
pkg: github.com/gin-contrib/logger
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
         │  before.txt   │              after.txt               │
         │    sec/op     │    sec/op     vs base                │
Logger      2.924µ ±  2%   1.617µ ± 12%  -44.70% (p=0.000 n=10)
Logger-2   1649.0n ±  2%   766.1n ±  1%  -53.54% (p=0.000 n=10)
Logger-4   1009.5n ± 11%   496.6n ± 12%  -50.80% (p=0.000 n=10)
Logger-8    853.4n ±  3%   396.4n ± 12%  -53.56% (p=0.000 n=10)
geomean     1.428µ         702.7n        -50.78%
  • Loading branch information
cedric-appdirect committed May 26, 2023
1 parent 75a62bd commit b7b9aca
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type config struct {
serverErrorLevel zerolog.Level
}

var isTerm bool = isatty.IsTerminal(os.Stdout.Fd())

Check failure on line 35 in logger.go

View workflow job for this annotation

GitHub Actions / lint

undeclared name: `isatty` (typecheck)

// SetLogger initializes the logging middleware.
func SetLogger(opts ...Option) gin.HandlerFunc {
cfg := &config{
Expand All @@ -56,7 +58,6 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
}

return func(c *gin.Context) {
isTerm := isatty.IsTerminal(os.Stdout.Fd())
l := zerolog.New(cfg.output).
Output(
zerolog.ConsoleWriter{
Expand Down

0 comments on commit b7b9aca

Please sign in to comment.