Skip to content

Commit

Permalink
🔥 Feature: Add AllLogger to Config
Browse files Browse the repository at this point in the history
  • Loading branch information
haochunchang committed Oct 2, 2024
1 parent 44cd700 commit ebadc5f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions middleware/logger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gofiber/fiber/v3"
fiberlog "github.com/gofiber/fiber/v3/log"
)

// Config defines the config for middleware.
Expand Down Expand Up @@ -43,6 +44,8 @@ type Config struct {
// Optional. Default: defaultLogger
LoggerFunc func(c fiber.Ctx, data *Data, cfg Config) error

Logger fiberlog.AllLogger

timeZoneLocation *time.Location

// Format defines the logging tags
Expand Down
40 changes: 40 additions & 0 deletions middleware/logger/default_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,51 @@ import (
"github.com/valyala/bytebufferpool"
)

func defaultLogger(c fiber.Ctx, data *Data, cfg Config) error {
colors := c.App().Config().ColorScheme

formatErr := ""
if cfg.enableColors {
if data.ChainErr != nil {
formatErr = colors.Red + " | " + data.ChainErr.Error() + colors.Reset
}
// use Logger interface method to replace fmt.Sprintf
cfg.Logger.Infof("%s |%s %3d %s| %13v | %15s |%s %-7s %s| ",
data.Timestamp.Load().(string), //nolint:forcetypeassert // Timestamp is always a string
statusColor(c.Response().StatusCode(), colors), c.Response().StatusCode(), colors.Reset,
data.Stop.Sub(data.Start),
c.IP(),
methodColor(c.Method(), colors), c.Method(), colors.Reset,
)
} else {
if data.ChainErr != nil {
formatErr = " | " + data.ChainErr.Error()
}
cfg.Logger.Infof("%s | %3d | %13v | %15s | %-7s | ",
data.Timestamp.Load().(string), //nolint:forcetypeassert // Timestamp is always a string
c.Response().StatusCode(),
data.Stop.Sub(data.Start).String(),
c.IP(),
c.Method(),
)
}
cfg.Logger.Errorf("%-"+data.ErrPaddingStr+"s %s\n", c.Path(), formatErr)
if cfg.Done != nil {
cfg.Done(c, []byte{})
}
return nil
}

// default logger for fiber
func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
// Alias colors
colors := c.App().Config().ColorScheme

if cfg.Logger != nil && cfg.Format == defaultFormat {
cfg.Logger.SetOutput(cfg.Output)
return defaultLogger(c, data, cfg)
}

// Get new buffer
buf := bytebufferpool.Get()

Expand Down
25 changes: 25 additions & 0 deletions middleware/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/gofiber/fiber/v3"
fiberlog "github.com/gofiber/fiber/v3/log"
"github.com/gofiber/fiber/v3/middleware/requestid"
"github.com/stretchr/testify/require"
"github.com/valyala/bytebufferpool"
Expand Down Expand Up @@ -181,6 +182,30 @@ func Test_Logger_ErrorTimeZone(t *testing.T) {
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
}

// go test -run Test_Logger_Fiber_Logger
func Test_Logger_Fiber_Logger(t *testing.T) {
t.Parallel()
app := fiber.New()

buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

app.Use(New(Config{
Format: "${error}",
Output: buf,
Logger: fiberlog.DefaultLogger(),
}))

app.Get("/", func(_ fiber.Ctx) error {
return errors.New("some random error")
})

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
require.Equal(t, fiber.StatusInternalServerError, resp.StatusCode)
require.Equal(t, "some random error", buf.String())
}

type fakeErrorOutput int

func (o *fakeErrorOutput) Write([]byte) (int, error) {
Expand Down

0 comments on commit ebadc5f

Please sign in to comment.