Skip to content

Commit

Permalink
🚨 Test: add tests for using fiber logger interface wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
haochunchang committed Oct 14, 2024
1 parent 9f406ae commit 3dd20b4
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions middleware/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ func Test_Logger_ErrorTimeZone(t *testing.T) {
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
}

type dumbLogger struct {
logger fiberlog.AllLogger
}

func (l *dumbLogger) Write(p []byte) (n int, err error) {

Check failure on line 189 in middleware/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / lint

named return "n" with type "int" found (nonamedreturns)

Check failure on line 189 in middleware/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / lint

named return "err" with type "error" found (nonamedreturns)
l.logger.Error(string(p))
return len(p), nil
}

func LoggerToWriter(customLogger fiberlog.AllLogger) io.Writer {
return &dumbLogger{logger: customLogger}
}

// go test -run Test_Logger_Fiber_Logger
func Test_Logger_Fiber_Logger(t *testing.T) {
t.Parallel()
Expand All @@ -190,17 +203,12 @@ func Test_Logger_Fiber_Logger(t *testing.T) {
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

customLoggerFunc := func(_ fiber.Ctx, data *Data, cfg Config) error {
cfg.Logger.SetOutput(cfg.Output)
cfg.Logger.SetFlags(0)
cfg.Logger.Error(data.ChainErr.Error())
return nil
}

logger := fiberlog.DefaultLogger()
logger.SetFlags(0)
logger.SetOutput(buf)
app.Use(New(Config{
Output: buf,
Logger: fiberlog.DefaultLogger(),
LoggerFunc: customLoggerFunc,
Format: "${error}",
Output: LoggerToWriter(logger),
}))

app.Get("/", func(_ fiber.Ctx) error {
Expand Down Expand Up @@ -763,19 +771,10 @@ func Benchmark_Logger(b *testing.B) {

b.Run("DefaultFormatWithFiberLog", func(bb *testing.B) {
app := fiber.New()
customLoggerFunc := func(c fiber.Ctx, data *Data, cfg Config) error {
cfg.Logger.SetOutput(cfg.Output)
cfg.Logger.Infof("%3d | %13v | %15s | %-7s | %-"+data.ErrPaddingStr+"s %s\n",
c.Response().StatusCode(),
data.Stop.Sub(data.Start),
c.IP(), c.Method(), c.Path(), "",
)
return nil
}
logger := fiberlog.DefaultLogger()
logger.SetOutput(io.Discard)
app.Use(New(Config{
Output: io.Discard,
Logger: fiberlog.DefaultLogger(),
LoggerFunc: customLoggerFunc,
Output: LoggerToWriter(logger),
}))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
Expand Down Expand Up @@ -928,19 +927,10 @@ func Benchmark_Logger_Parallel(b *testing.B) {

b.Run("DefaultFormatWithFiberLog", func(bb *testing.B) {
app := fiber.New()
customLoggerFunc := func(c fiber.Ctx, data *Data, cfg Config) error {
cfg.Logger.SetOutput(cfg.Output)
cfg.Logger.Infof("%3d | %13v | %15s | %-7s | %-"+data.ErrPaddingStr+"s %s\n",
c.Response().StatusCode(),
data.Stop.Sub(data.Start),
c.IP(), c.Method(), c.Path(), "",
)
return nil
}
logger := fiberlog.DefaultLogger()
logger.SetOutput(io.Discard)
app.Use(New(Config{
Output: io.Discard,
Logger: fiberlog.DefaultLogger(),
LoggerFunc: customLoggerFunc,
Output: LoggerToWriter(logger),
}))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
Expand Down

0 comments on commit 3dd20b4

Please sign in to comment.