27 lines
571 B
Go
27 lines
571 B
Go
package log_mw
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/themakers/hlog"
|
|
"strings"
|
|
)
|
|
|
|
const HlogCtxKey string = "logger"
|
|
|
|
func ContextLogger(logger hlog.Logger) fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
c.Locals(HlogCtxKey, logger.With(map[string]string{
|
|
"ctxuserip": c.IP(),
|
|
"ctxuserport": c.Port(),
|
|
"keydomain": strings.Join(c.Subdomains(), "/"),
|
|
"keypath": c.Path(),
|
|
}))
|
|
return c.Next()
|
|
}
|
|
}
|
|
|
|
func ExtractLogger(ctx *fiber.Ctx) hlog.Logger {
|
|
logger := ctx.Context().UserValue(HlogCtxKey).(hlog.Logger)
|
|
return logger
|
|
}
|