-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
logger.go
39 lines (33 loc) · 1 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package middlewares
import (
"time"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
"exusiai.dev/backend-next/internal/pkg/flog"
)
func Logger(app *fiber.App) {
Chained(
app,
injectLogger(),
flog.RequestIDHandler("http.request_id", "X-Penguin-Request-ID"),
flog.PenguinIDHandler("usr.id"),
flog.RemoteAddrHandler("network.client.ip"),
flog.MethodHandler("http.method"),
flog.URLHandler("http.url"),
requestLogger(),
)
}
func injectLogger() func(ctx *fiber.Ctx) error {
return flog.NewHandlerMiddleware(log.With().Logger())
}
func requestLogger() func(ctx *fiber.Ctx) error {
return flog.AccessHandler(func(ctx *fiber.Ctx, duration time.Duration) {
flog.InfoFrom(ctx, "http.request").
Bytes("http.useragent", ctx.Request().Header.UserAgent()).
Int("http.status_code", ctx.Response().StatusCode()).
Int("network.bytes_read", len(ctx.Request().Body())).
Int("network.bytes_written", len(ctx.Response().Body())).
Dur("duration", duration).
Msg("received request")
})
}