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

♻️ Refactor: Remove mutex lock in logger middleware #2840

Merged
merged 1 commit into from
Feb 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/api/middleware/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ app.Use(logger.New(logger.Config{
}))
```

:::tip
Writing to os.File is goroutine-safe, but if you are using a custom Output that is not goroutine-safe, make sure to implement locking to properly serialize writes.
:::

## Config

### Config
Expand Down
5 changes: 0 additions & 5 deletions middleware/logger/default_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"os"
"strconv"
"sync"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/utils/v2"
Expand All @@ -15,8 +14,6 @@ import (
"github.com/valyala/fasthttp"
)

var mu sync.Mutex

// default logger for fiber
func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
// Alias colors
Expand Down Expand Up @@ -128,9 +125,7 @@ func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
_, _ = buf.WriteString(err.Error()) //nolint:errcheck // This will never fail
}

mu.Lock()
writeLog(cfg.Output, buf.Bytes())
mu.Unlock()

if cfg.Done != nil {
cfg.Done(c, buf.Bytes())
Expand Down
Loading