common/log_mw/hlog_mw.go
2024-06-07 20:18:09 +00:00

26 lines
572 B
Go

package log_mw
import (
"github.com/gofiber/fiber/v2"
"github.com/themakers/hlog"
)
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.Get("X-Real-IP"),
"ctxuserport": c.Get("X-Client-Port"),
"keydomain": c.Hostname(),
"keypath": c.Get("Origin"),
}))
return c.Next()
}
}
func ExtractLogger(ctx *fiber.Ctx) hlog.Logger {
logger := ctx.Context().UserValue(HlogCtxKey).(hlog.Logger)
return logger
}