Skip to content

Commit 2788a53

Browse files
committed
🐛 bug: Use Content-Length for bytesReceived and bytesSent tags in Logger Middleware (#3066)
* logger: Use Content-Length header for BytesReceived and BytesSent tags * Use strconv.AppendInt instead of fasthttp.AppendUint
1 parent abf1898 commit 2788a53

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

middleware/logger/default_logger.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/mattn/go-colorable"
1212
"github.com/mattn/go-isatty"
1313
"github.com/valyala/bytebufferpool"
14-
"github.com/valyala/fasthttp"
1514
)
1615

1716
// default logger for fiber
@@ -151,7 +150,7 @@ func beforeHandlerFunc(cfg Config) {
151150

152151
func appendInt(output Buffer, v int) (int, error) {
153152
old := output.Len()
154-
output.Set(fasthttp.AppendUint(output.Bytes(), v))
153+
output.Set(strconv.AppendInt(output.Bytes(), int64(v), 10))
155154
return output.Len() - old, nil
156155
}
157156

middleware/logger/logger_test.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,40 @@ func Test_Response_Body(t *testing.T) {
407407
require.Equal(t, expectedGetResponse, buf.String())
408408

409409
buf.Reset() // Reset buffer to test POST
410-
411410
_, err = app.Test(httptest.NewRequest(fiber.MethodPost, "/test", nil))
412-
require.NoError(t, err)
413411

414412
expectedPostResponse := "Post in test"
413+
require.NoError(t, err)
415414
require.Equal(t, expectedPostResponse, buf.String())
416415
}
417416

417+
// go test -run Test_Request_Body
418+
func Test_Request_Body(t *testing.T) {
419+
t.Parallel()
420+
buf := bytebufferpool.Get()
421+
defer bytebufferpool.Put(buf)
422+
app := fiber.New()
423+
424+
app.Use(New(Config{
425+
Format: "${bytesReceived} ${bytesSent} ${status}",
426+
Output: buf,
427+
}))
428+
429+
app.Post("/", func(c fiber.Ctx) error {
430+
c.Response().Header.SetContentLength(5)
431+
return c.SendString("World")
432+
})
433+
434+
// Create a POST request with a body
435+
body := []byte("Hello")
436+
req := httptest.NewRequest(fiber.MethodPost, "/", bytes.NewReader(body))
437+
req.Header.Set("Content-Type", "application/octet-stream")
438+
439+
_, err := app.Test(req)
440+
require.NoError(t, err)
441+
require.Equal(t, "5 5 200", buf.String())
442+
}
443+
418444
// go test -run Test_Logger_AppendUint
419445
func Test_Logger_AppendUint(t *testing.T) {
420446
t.Parallel()
@@ -432,10 +458,21 @@ func Test_Logger_AppendUint(t *testing.T) {
432458
return c.SendString("hello")
433459
})
434460

461+
app.Get("/content", func(c fiber.Ctx) error {
462+
c.Response().Header.SetContentLength(5)
463+
return c.SendString("hello")
464+
})
465+
435466
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
436467
require.NoError(t, err)
437468
require.Equal(t, fiber.StatusOK, resp.StatusCode)
438-
require.Equal(t, "0 5 200", buf.String())
469+
require.Equal(t, "-2 0 200", buf.String())
470+
471+
buf.Reset()
472+
resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/content", nil))
473+
require.NoError(t, err)
474+
require.Equal(t, fiber.StatusOK, resp.StatusCode)
475+
require.Equal(t, "-2 5 200", buf.String())
439476
}
440477

441478
// go test -run Test_Logger_Data_Race -race
@@ -618,7 +655,9 @@ func Test_Logger_ByteSent_Streaming(t *testing.T) {
618655
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
619656
require.NoError(t, err)
620657
require.Equal(t, fiber.StatusOK, resp.StatusCode)
621-
require.Equal(t, "0 0 200", buf.String())
658+
659+
// -2 means identity, -1 means chunked, 200 status
660+
require.Equal(t, "-2 -1 200", buf.String())
622661
}
623662

624663
type fakeOutput int

middleware/logger/tags.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,10 @@ func createTagMap(cfg *Config) map[string]LogFunc {
8787
return output.Write(c.Body())
8888
},
8989
TagBytesReceived: func(output Buffer, c fiber.Ctx, _ *Data, _ string) (int, error) {
90-
return appendInt(output, len(c.Request().Body()))
90+
return appendInt(output, c.Request().Header.ContentLength())
9191
},
9292
TagBytesSent: func(output Buffer, c fiber.Ctx, _ *Data, _ string) (int, error) {
93-
if c.Response().Header.ContentLength() < 0 {
94-
return appendInt(output, 0)
95-
}
96-
return appendInt(output, len(c.Response().Body()))
93+
return appendInt(output, c.Response().Header.ContentLength())
9794
},
9895
TagRoute: func(output Buffer, c fiber.Ctx, _ *Data, _ string) (int, error) {
9996
return output.WriteString(c.Route().Path)

0 commit comments

Comments
 (0)