From 43246d257eb6b3b58cab193a32821c94344a671c Mon Sep 17 00:00:00 2001 From: Marvin Preuss Date: Wed, 23 Mar 2022 13:54:44 +0100 Subject: [PATCH] refactor!: sets an default context logger and returns now pointers of zerolog.Logger --- logginghandler.go | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/logginghandler.go b/logginghandler.go index c035d3c..d4dd0a6 100644 --- a/logginghandler.go +++ b/logginghandler.go @@ -13,6 +13,10 @@ import ( "github.com/rs/zerolog/log" ) +func init() { //nolint:gochecknoinits + zerolog.DefaultContextLogger = &log.Logger +} + // GetUUID gets the requests UUID from a request. func GetUUID(r *http.Request) (string, bool) { uuid, ok := hlog.IDFromRequest(r) @@ -23,28 +27,16 @@ func GetUUID(r *http.Request) (string, bool) { 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. -func FromRequest(r *http.Request) zerolog.Logger { - l := hlog.FromRequest(r) - - if l.GetLevel() == zerolog.Disabled { - return log.Logger - } - - return *hlog.FromRequest(r) +func FromRequest(r *http.Request) *zerolog.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. -func FromCtx(ctx context.Context) zerolog.Logger { - l := *log.Ctx(ctx) - - if l.GetLevel() == zerolog.Disabled { - return log.Logger - } - - return l +func FromCtx(ctx context.Context) *zerolog.Logger { + return zerolog.Ctx(ctx) } func Handler(log zerolog.Logger) func(http.Handler) http.Handler {