diff --git a/zap.go b/zap.go index b6b155f..d9af527 100644 --- a/zap.go +++ b/zap.go @@ -152,7 +152,7 @@ func CustomRecoveryWithZap(logger ZapLogger, stack bool, recovery gin.RecoveryFu zap.String("request", string(httpRequest)), ) // If the connection is dead, we can't write a status to it. - c.Error(err.(error)) // nolint: errcheck + c.Error(err.(error)) //nolint: errcheck c.Abort() return } diff --git a/zap_test.go b/zap_test.go index be1cbad..0e81ca3 100644 --- a/zap_test.go +++ b/zap_test.go @@ -1,6 +1,7 @@ package ginzap import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -14,6 +15,8 @@ import ( "go.uber.org/zap/zaptest/observer" ) +const testPath = "/test" + func buildDummyLogger() (*zap.Logger, *observer.ObservedLogs) { core, obs := observer.New(zap.InfoLevel) logger := zap.New(core) @@ -33,6 +36,8 @@ func timestampLocationCheck(timestampStr string, location *time.Location) error } func TestGinzap(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() r := gin.New() utcLogger, utcLoggerObserved := buildDummyLogger() @@ -41,12 +46,12 @@ func TestGinzap(t *testing.T) { localLogger, localLoggerObserved := buildDummyLogger() r.Use(Ginzap(localLogger, time.RFC3339, false)) - r.GET("/test", func(c *gin.Context) { + r.GET(testPath, func(c *gin.Context) { c.JSON(204, nil) }) res1 := httptest.NewRecorder() - req1, _ := http.NewRequest("GET", "/test", nil) + req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil) r.ServeHTTP(res1, req1) if len(utcLoggerObserved.All()) != 1 { @@ -55,7 +60,7 @@ func TestGinzap(t *testing.T) { logLine := utcLoggerObserved.All()[0] pathStr := logLine.Context[2].String - if pathStr != "/test" { + if pathStr != testPath { t.Fatalf("logged path should be /test but %s", pathStr) } @@ -70,12 +75,15 @@ func TestGinzap(t *testing.T) { logLine = localLoggerObserved.All()[0] pathStr = logLine.Context[2].String - if pathStr != "/test" { + if pathStr != testPath { t.Fatalf("logged path should be /test but %s", pathStr) } } func TestGinzapWithConfig(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + r := gin.New() utcLogger, utcLoggerObserved := buildDummyLogger() @@ -86,7 +94,7 @@ func TestGinzapWithConfig(t *testing.T) { DefaultLevel: zapcore.WarnLevel, })) - r.GET("/test", func(c *gin.Context) { + r.GET(testPath, func(c *gin.Context) { c.JSON(204, nil) }) @@ -95,11 +103,11 @@ func TestGinzapWithConfig(t *testing.T) { }) res1 := httptest.NewRecorder() - req1, _ := http.NewRequest("GET", "/test", nil) + req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil) r.ServeHTTP(res1, req1) res2 := httptest.NewRecorder() - req2, _ := http.NewRequest("GET", "/no_log", nil) + req2, _ := http.NewRequestWithContext(ctx, "GET", "/no_log", nil) r.ServeHTTP(res2, req2) if res2.Code != 204 { @@ -112,7 +120,7 @@ func TestGinzapWithConfig(t *testing.T) { logLine := utcLoggerObserved.All()[0] pathStr := logLine.Context[2].String - if pathStr != "/test" { + if pathStr != testPath { t.Fatalf("logged path should be /test but %s", pathStr) } @@ -127,6 +135,8 @@ func TestGinzapWithConfig(t *testing.T) { } func TestLoggerSkipper(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() r := gin.New() utcLogger, utcLoggerObserved := buildDummyLogger() @@ -138,7 +148,7 @@ func TestLoggerSkipper(t *testing.T) { }, })) - r.GET("/test", func(c *gin.Context) { + r.GET(testPath, func(c *gin.Context) { c.JSON(204, nil) }) @@ -147,11 +157,11 @@ func TestLoggerSkipper(t *testing.T) { }) res1 := httptest.NewRecorder() - req1, _ := http.NewRequest("GET", "/test", nil) + req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil) r.ServeHTTP(res1, req1) res2 := httptest.NewRecorder() - req2, _ := http.NewRequest("GET", "/no_log", nil) + req2, _ := http.NewRequestWithContext(ctx, "GET", "/no_log", nil) r.ServeHTTP(res2, req2) if res2.Code != 204 { @@ -164,7 +174,7 @@ func TestLoggerSkipper(t *testing.T) { logLine := utcLoggerObserved.All()[0] pathStr := logLine.Context[2].String - if pathStr != "/test" { + if pathStr != testPath { t.Fatalf("logged path should be /test but %s", pathStr) } }