mirror of https://github.com/gofiber/fiber.git
♻️ 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
parent
9fd8bfc0bf
commit
70067a1754
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in New Issue