From 0aada801089ad89ce550b890445d819ce9738968 Mon Sep 17 00:00:00 2001 From: Marvin Preuss Date: Fri, 18 Feb 2022 15:41:57 +0100 Subject: [PATCH] feat!: FromRequest and FromCtx will return the global logger if the one extracted is disabled --- logginghandler.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/logginghandler.go b/logginghandler.go index d65ab62..c035d3c 100644 --- a/logginghandler.go +++ b/logginghandler.go @@ -24,13 +24,27 @@ func GetUUID(r *http.Request) (string, bool) { } // FromRequest returns a logger with the UUID 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) } // FromCtx returns a logger with the UUID set from ctx. +// If no one could be found, it will return the global one. func FromCtx(ctx context.Context) zerolog.Logger { - return *log.Ctx(ctx) + l := *log.Ctx(ctx) + + if l.GetLevel() == zerolog.Disabled { + return log.Logger + } + + return l } func Handler(log zerolog.Logger) func(http.Handler) http.Handler {