refactor!: sets an default context logger and returns now pointers of zerolog.Logger
This commit is contained in:
parent
59613e6475
commit
43246d257e
@ -13,6 +13,10 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() { //nolint:gochecknoinits
|
||||||
|
zerolog.DefaultContextLogger = &log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
// GetUUID gets the requests UUID from a request.
|
// GetUUID gets the requests UUID from a request.
|
||||||
func GetUUID(r *http.Request) (string, bool) {
|
func GetUUID(r *http.Request) (string, bool) {
|
||||||
uuid, ok := hlog.IDFromRequest(r)
|
uuid, ok := hlog.IDFromRequest(r)
|
||||||
@ -23,28 +27,16 @@ func GetUUID(r *http.Request) (string, bool) {
|
|||||||
return uuid.String(), true
|
return uuid.String(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromRequest returns a logger with the UUID set from request.
|
// FromRequest returns a logger set from request.
|
||||||
// If no one could be found, it will return the global one.
|
// If no one could be found, it will return the global one.
|
||||||
func FromRequest(r *http.Request) zerolog.Logger {
|
func FromRequest(r *http.Request) *zerolog.Logger {
|
||||||
l := hlog.FromRequest(r)
|
return hlog.FromRequest(r)
|
||||||
|
|
||||||
if l.GetLevel() == zerolog.Disabled {
|
|
||||||
return log.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
return *hlog.FromRequest(r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromCtx returns a logger with the UUID set from ctx.
|
// FromCtx returns a logger set from ctx.
|
||||||
// If no one could be found, it will return the global one.
|
// If no one could be found, it will return the global one.
|
||||||
func FromCtx(ctx context.Context) zerolog.Logger {
|
func FromCtx(ctx context.Context) *zerolog.Logger {
|
||||||
l := *log.Ctx(ctx)
|
return zerolog.Ctx(ctx)
|
||||||
|
|
||||||
if l.GetLevel() == zerolog.Disabled {
|
|
||||||
return log.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
return l
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Handler(log zerolog.Logger) func(http.Handler) http.Handler {
|
func Handler(log zerolog.Logger) func(http.Handler) http.Handler {
|
||||||
|
Loading…
Reference in New Issue
Block a user