From 433c5bfd77201e8bef3bf79ed9216d341ea8ada3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 5 Oct 2022 11:37:52 +0200 Subject: [PATCH] feat: add context funcs to logging package (#4812) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché Signed-off-by: Charles-Edouard Brétéché --- cmd/initContainer/main.go | 2 +- cmd/kyverno/main.go | 2 +- pkg/logging/log.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/cmd/initContainer/main.go b/cmd/initContainer/main.go index bed1c42982..1066146519 100644 --- a/cmd/initContainer/main.go +++ b/cmd/initContainer/main.go @@ -70,7 +70,7 @@ func main() { os.Exit(1) } // os signal handler - signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + signalCtx, signalCancel := signal.NotifyContext(logging.Background(), os.Interrupt, syscall.SIGTERM) defer signalCancel() stopCh := signalCtx.Done() diff --git a/cmd/kyverno/main.go b/cmd/kyverno/main.go index 526f13cdfd..c6b7ffaf7e 100644 --- a/cmd/kyverno/main.go +++ b/cmd/kyverno/main.go @@ -238,7 +238,7 @@ func main() { // show startup message showStartup(logger) // os signal handler - signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + signalCtx, signalCancel := signal.NotifyContext(logging.IntoBackground(logger), os.Interrupt, syscall.SIGTERM) defer signalCancel() // setup metrics diff --git a/pkg/logging/log.go b/pkg/logging/log.go index f7d5be6a15..885f685102 100644 --- a/pkg/logging/log.go +++ b/pkg/logging/log.go @@ -1,6 +1,7 @@ package logging import ( + "context" "errors" "flag" "os" @@ -79,3 +80,38 @@ func Info(msg string, keysAndValues ...interface{}) { func Error(err error, msg string, keysAndValues ...interface{}) { GlobalLogger().Error(err, msg, keysAndValues...) } + +// FromContext returns a logger with predefined values from a context.Context. +func FromContext(ctx context.Context, keysAndValues ...interface{}) (logr.Logger, error) { + logger, err := logr.FromContext(ctx) + if err != nil { + return logger, err + } + return logger.WithValues(keysAndValues...), nil +} + +// IntoContext takes a context and sets the logger as one of its values. +// Use FromContext function to retrieve the logger. +func IntoContext(ctx context.Context, log logr.Logger) context.Context { + return logr.NewContext(ctx, log) +} + +// IntoBackground calls IntoContext with the logger and a Background context. +func IntoBackground(log logr.Logger) context.Context { + return IntoContext(context.Background(), log) +} + +// IntoTODO calls IntoContext with the logger and a TODO context. +func IntoTODO(log logr.Logger) context.Context { + return IntoContext(context.TODO(), log) +} + +// Background calls IntoContext with the global logger and a Background context. +func Background() context.Context { + return IntoContext(context.Background(), GlobalLogger()) +} + +// TODO calls IntoContext with the global logger and a TODO context. +func TODO() context.Context { + return IntoContext(context.TODO(), GlobalLogger()) +}