From 5408a80a24d6f7ebac894936bf25ad9e37044e73 Mon Sep 17 00:00:00 2001 From: Arpad Kiss Date: Wed, 9 Oct 2024 15:25:54 +0200 Subject: [PATCH] Add capability to set loglevel to trace during runtime Signed-off-by: Arpad Kiss --- main.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index c8aa49e..ce5fe8c 100644 --- a/main.go +++ b/main.go @@ -103,11 +103,7 @@ func main() { logrus.Fatalf("error processing config from env: %+v", err) } - l, err := logrus.ParseLevel(config.LogLevel) - if err != nil { - logrus.Fatalf("invalid log level %s", config.LogLevel) - } - logrus.SetLevel(l) + setupLogLevel(ctx, config.LogLevel) log.FromContext(ctx).Infof("Config: %#v", config) @@ -118,7 +114,7 @@ func main() { metricExporter := opentelemetry.InitOPTLMetricExporter(ctx, collectorAddress, config.MetricsExportInterval) o := opentelemetry.Init(ctx, spanExporter, metricExporter, "registry-proxy-dns") defer func() { - if err = o.Close(); err != nil { + if err := o.Close(); err != nil { log.FromContext(ctx).Error(err.Error()) } }() @@ -196,3 +192,15 @@ func exitOnErr(ctx context.Context, cancel context.CancelFunc, errCh <-chan erro cancel() }(ctx, errCh) } + +func setupLogLevel(ctx context.Context, logLevel string) { + l, err := logrus.ParseLevel(logLevel) + if err != nil { + logrus.Fatalf("invalid log level %s", logLevel) + } + logrus.SetLevel(l) + logruslogger.SetupLevelChangeOnSignal(ctx, map[os.Signal]logrus.Level{ + syscall.SIGUSR1: logrus.TraceLevel, + syscall.SIGUSR2: l, + }) +}