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

While not all implementations of io.Write will be goroutine safe, the
vast majority of users of the logger middleware are likely to use
os.File, which does implement safe concurrent writes. If users require
locking, they can implement this on an as-needed basis. The risk of
having global locking is that a slow write can hold up the entire
server.
pull/2846/head
nickajacks1 2024-02-09 16:32:37 -08:00 committed by GitHub
parent 9fd8bfc0bf
commit 70067a1754
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 5 deletions

View File

@ -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

View File

@ -5,7 +5,6 @@ import (
"io"
"os"
"strconv"
"sync"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/utils/v2"
@ -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
@ -128,9 +125,7 @@ func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
buf.WriteString(err.Error())
}
mu.Lock()
writeLog(cfg.Output, buf.Bytes())
mu.Unlock()
if cfg.Done != nil {
cfg.Done(c, buf.Bytes())