From 289b4b1f9c0926d16e0e3e569d387187b7910606 Mon Sep 17 00:00:00 2001 From: Slach Date: Sun, 4 Aug 2024 12:48:48 +0400 Subject: [PATCH] fix corner case for LOG_LEVEL + --env, fix https://github.com/Altinity/clickhouse-backup/issues/972 --- ChangeLog.md | 4 ++++ pkg/config/config.go | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 77bef4fb..a4fe5d66 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,7 @@ +# v2.5.23 +BUG FIXES +- fix corner case for LOG_LEVEL + --env, fix [972](https://github.com/Altinity/clickhouse-backup/issues/972) + # v2.5.22 IMPROVEMENTS - redirect logs into stderr instead of stdout, fix [969](https://github.com/Altinity/clickhouse-backup/issues/969) diff --git a/pkg/config/config.go b/pkg/config/config.go index b4556c18..40c012bf 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -660,12 +660,29 @@ type oldEnvValues struct { func OverrideEnvVars(ctx *cli.Context) map[string]oldEnvValues { env := ctx.StringSlice("env") oldValues := map[string]oldEnvValues{} + logLevel := "info" + if os.Getenv("LOG_LEVEL") != "" { + logLevel = os.Getenv("LOG_LEVEL") + } + log_helper.SetLogLevelFromString(logLevel) if len(env) > 0 { - for _, v := range env { - envVariable := strings.SplitN(v, "=", 2) - if len(envVariable) < 2 { - envVariable = append(envVariable, "true") + processEnvFromCli := func(process func(envVariable []string)) { + for _, v := range env { + envVariable := strings.SplitN(v, "=", 2) + if len(envVariable) < 2 { + envVariable = append(envVariable, "true") + } + process(envVariable) + } + } + + processEnvFromCli(func(envVariable []string) { + if envVariable[0] == "LOG_LEVEL" { + log_helper.SetLogLevelFromString(envVariable[1]) } + }) + + processEnvFromCli(func(envVariable []string) { log.Info().Msgf("override %s=%s", envVariable[0], envVariable[1]) oldValue, wasPresent := os.LookupEnv(envVariable[0]) oldValues[envVariable[0]] = oldEnvValues{ @@ -675,7 +692,7 @@ func OverrideEnvVars(ctx *cli.Context) map[string]oldEnvValues { if err := os.Setenv(envVariable[0], envVariable[1]); err != nil { log.Warn().Msgf("can't override %s=%s, error: %v", envVariable[0], envVariable[1], err) } - } + }) } return oldValues }