Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add callback function for middleware/logger #2219

Merged
merged 2 commits into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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