Skip to content

Commit

Permalink
✨ Add callback function for middleware/logger (#2219)
Browse files Browse the repository at this point in the history
* ✨ Add callback function for middleware/logger

* Refine test code
  • Loading branch information
panjf2000 committed Nov 15, 2022
1 parent 61b4496 commit 3157fb5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
10 changes: 10 additions & 0 deletions middleware/logger/config.go
Expand Up @@ -16,6 +16,12 @@ type Config struct {
// Optional. Default: nil
Next func(c *fiber.Ctx) bool

// Done is a function that is called after the log string for a request is written to Output,
// and pass the log string as parameter.
//
// Optional. Default: a function that does nothing.
Done func(c *fiber.Ctx, logString []byte)

// Format defines the logging tags
//
// Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n
Expand Down Expand Up @@ -49,6 +55,7 @@ type Config struct {
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Done: func(c *fiber.Ctx, logString []byte) {},
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
TimeFormat: "15:04:05",
TimeZone: "Local",
Expand Down Expand Up @@ -90,6 +97,9 @@ func configDefault(config ...Config) Config {
if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}
if cfg.Done == nil {
cfg.Done = ConfigDefault.Done
}
if cfg.Format == "" {
cfg.Format = ConfigDefault.Format
}
Expand Down
12 changes: 9 additions & 3 deletions middleware/logger/logger.go
Expand Up @@ -10,12 +10,13 @@ import (
"sync/atomic"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/internal/bytebufferpool"
"github.com/gofiber/fiber/v2/internal/fasttemplate"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/valyala/fasthttp"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/internal/bytebufferpool"
"github.com/gofiber/fiber/v2/internal/fasttemplate"
)

// Logger variables
Expand Down Expand Up @@ -186,6 +187,8 @@ func New(config ...Config) fiber.Handler {
// Write buffer to output
_, _ = cfg.Output.Write(buf.Bytes())

cfg.Done(c, buf.Bytes())

// Put buffer back to pool
bytebufferpool.Put(buf)

Expand Down Expand Up @@ -315,6 +318,9 @@ func New(config ...Config) fiber.Handler {
}
}
mu.Unlock()

cfg.Done(c, buf.Bytes())

// Put buffer back to pool
bytebufferpool.Put(buf)

Expand Down
25 changes: 24 additions & 1 deletion middleware/logger/logger_test.go
@@ -1,6 +1,7 @@
package logger

import (
"bytes"
"errors"
"fmt"
"io"
Expand All @@ -10,11 +11,12 @@ import (
"sync"
"testing"

"github.com/valyala/fasthttp"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/internal/bytebufferpool"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)

// go test -run Test_Logger
Expand Down Expand Up @@ -99,6 +101,27 @@ func Test_Logger_Next(t *testing.T) {
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
}

// go test -run Test_Logger_Done
func Test_Logger_Done(t *testing.T) {
buf := bytes.NewBuffer(nil)
app := fiber.New()
app.Use(New(Config{
Done: func(c *fiber.Ctx, logString []byte) {
if c.Response().StatusCode() == fiber.StatusOK {
buf.Write(logString)
}
},
})).Get("/logging", func(ctx *fiber.Ctx) error {
return ctx.SendStatus(fiber.StatusOK)
})

resp, err := app.Test(httptest.NewRequest("GET", "/logging", nil))

utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, true, buf.Len() > 0)
}

// go test -run Test_Logger_ErrorTimeZone
func Test_Logger_ErrorTimeZone(t *testing.T) {
app := fiber.New()
Expand Down

1 comment on commit 3157fb5

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3157fb5 Previous: 61b4496 Ratio
Benchmark_TrimRight/default 13.15 ns/op 0 B/op 0 allocs/op 5.467 ns/op 0 B/op 0 allocs/op 2.41

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.