diff --git a/middleware_test.go b/middleware_test.go index 59d4c02..14bc7e3 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -116,6 +116,36 @@ func TestMiddleware(t *testing.T) { RequestLatencyLevel: zerolog.WarnLevel, }) + // Slow request should be logged at the escalated level + next := func(c echo.Context) error { + time.Sleep(5 * time.Millisecond) + return nil + } + handler := m(next) + err := handler(c) + assert.NoError(t, err, "should not return error") + + str := b.String() + assert.Contains(t, str, `"level":"warn"`) + assert.NotContains(t, str, `"level":"info"`) + }) + + t.Run("shouldn't escalate log level for fast requests", func(t *testing.T) { + e := echo.New() + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + b := &bytes.Buffer{} + l := lecho.New(b) + l.SetLevel(log.INFO) + m := lecho.Middleware(lecho.Config{ + Logger: l, + RequestLatencyLimit: 5 * time.Millisecond, + RequestLatencyLevel: zerolog.WarnLevel, + }) + // Fast request should be logged at the default level next := func(c echo.Context) error { time.Sleep(1 * time.Millisecond) @@ -130,17 +160,5 @@ func TestMiddleware(t *testing.T) { str := b.String() assert.Contains(t, str, `"level":"info"`) assert.NotContains(t, str, `"level":"warn"`) - - // Slow request should be logged at the escalated level - next = func(c echo.Context) error { - time.Sleep(5 * time.Millisecond) - return nil - } - handler = m(next) - err = handler(c) - - str = b.String() - assert.Contains(t, str, `"level":"warn"`) }) - }