Skip to content

Commit

Permalink
šŸ› bug: Use Content-Length for bytesReceived and bytesSent tags in Logā€¦
Browse files Browse the repository at this point in the history
ā€¦ger Middleware in v2 (#3067)

Use Content-Length for bytesSent and bytesReceived in Logger
  • Loading branch information
gaby authored Jul 18, 2024
1 parent 6968d51 commit 1c52689
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
5 changes: 3 additions & 2 deletions middleware/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,14 @@ func Test_Logger_AppendUint(t *testing.T) {
}))

app.Get("/", func(c *fiber.Ctx) error {
c.Response().Header.SetContentLength(5)
return c.SendString("hello")
})

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "0 5 200", buf.String())
utils.AssertEqual(t, "-2 5 200", buf.String())
}

// go test -run Test_Logger_Data_Race -race
Expand Down Expand Up @@ -629,7 +630,7 @@ func Test_Logger_ByteSent_Streaming(t *testing.T) {
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "0 0 200", buf.String())
utils.AssertEqual(t, "-2 -1 200", buf.String())
}

// go test -run Test_Logger_EnableColors
Expand Down
8 changes: 3 additions & 5 deletions middleware/logger/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"fmt"
"strconv"
"strings"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -85,13 +86,10 @@ func createTagMap(cfg *Config) map[string]LogFunc {
return output.Write(c.Body())
},
TagBytesReceived: func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (int, error) {
return appendInt(output, len(c.Request().Body()))
return output.WriteString(strconv.Itoa((c.Request().Header.ContentLength())))
},
TagBytesSent: func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (int, error) {
if c.Response().Header.ContentLength() < 0 {
return appendInt(output, 0)
}
return appendInt(output, len(c.Response().Body()))
return output.WriteString(strconv.Itoa((c.Response().Header.ContentLength())))
},
TagRoute: func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (int, error) {
return output.WriteString(c.Route().Path)
Expand Down

0 comments on commit 1c52689

Please sign in to comment.