Skip to content

Commit

Permalink
fix(isatty): bugs/isatty hot path (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-appdirect committed Jul 9, 2023
1 parent 809ceee commit 75cf999
Show file tree
Hide file tree
Showing 2 changed files with 23 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
21 changes: 21 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,24 @@ func TestLoggerParseLevel(t *testing.T) {
})
}
}

func BenchmarkLogger(b *testing.B) {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(SetLogger(WithDefaultLevel(zerolog.Disabled)))
r.GET("/", func(ctx *gin.Context) {
ctx.Data(200, "text/plain", []byte("all good"))
})

b.ReportAllocs()
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()

for pb.Next() {
r.ServeHTTP(w, req)
}
})
}

0 comments on commit 75cf999

Please sign in to comment.