From 0fddbfb7ed260a8685eaa7f00a6939740c866c21 Mon Sep 17 00:00:00 2001 From: Matthew McNeely Date: Sat, 17 Sep 2022 06:12:24 -0400 Subject: [PATCH] feat: allow log level to be set to any level (#1345) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- cmd/root.go | 10 +++++----- docs/arguments.md | 21 ++++++++++++++++++++- internal/flags/flags.go | 22 ++++++++++++++++++++++ internal/flags/flags_test.go | 28 +++++++++++++++++++++++----- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index daa9437..f79b660 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -87,11 +87,11 @@ func PreRun(cmd *cobra.Command, _ []string) { }) } - if enabled, _ := f.GetBool("debug"); enabled { - log.SetLevel(log.DebugLevel) - } - if enabled, _ := f.GetBool("trace"); enabled { - log.SetLevel(log.TraceLevel) + rawLogLevel, _ := f.GetString(`log-level`) + if logLevel, err := log.ParseLevel(rawLogLevel); err != nil { + log.Fatalf("Invalid log level: %s", err.Error()) + } else { + log.SetLevel(logLevel) } scheduleSpec, _ = f.GetString("schedule") diff --git a/docs/arguments.md b/docs/arguments.md index 7280631..58e7e7b 100644 --- a/docs/arguments.md +++ b/docs/arguments.md @@ -71,6 +71,10 @@ Environment Variable: WATCHTOWER_REMOVE_VOLUMES ## Debug Enable debug mode with verbose logging. +!!! note "Notes" + Alias for `--log-level debug`. See [Maximum log level](#maximum-log-level). + Does _not_ take an argument when used as an argument. Using `--debug true` will **not** work. + ```text Argument: --debug, -d Environment Variable: WATCHTOWER_DEBUG @@ -81,6 +85,10 @@ Environment Variable: WATCHTOWER_DEBUG ## Trace Enable trace mode with very verbose logging. Caution: exposes credentials! +!!! note "Notes" + Alias for `--log-level trace`. See [Maximum log level](#maximum-log-level). + Does _not_ take an argument when used as an argument. Using `--trace true` will **not** work. + ```text Argument: --trace Environment Variable: WATCHTOWER_TRACE @@ -88,6 +96,17 @@ Environment Variable: WATCHTOWER_TRACE Default: false ``` +## Maximum log level + +The maximum log level that will be written to STDERR (shown in `docker log` when used in a container). + +```text + Argument: --log-level +Environment Variable: WATCHTOWER_LOG_LEVEL + Possible values: panic, fatal, error, warn, info, debug or trace + Default: info +``` + ## ANSI colors Disable ANSI color escape codes in log output. @@ -341,4 +360,4 @@ requests and may rate limit pull requests (mainly docker.io). Environment Variable: WATCHTOWER_WARN_ON_HEAD_FAILURE Possible values: always, auto, never Default: auto -``` +``` \ No newline at end of file diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 5abd69c..5428b95 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -182,6 +182,10 @@ func RegisterSystemFlags(rootCmd *cobra.Command) { viper.GetString("WATCHTOWER_PORCELAIN"), `Write session results to stdout using a stable versioned format. Supported values: "v1"`) + flags.String( + "log-level", + viper.GetString("WATCHTOWER_LOG_LEVEL"), + "The maximum log level that will be written to STDERR. Possible values: panic, fatal, error, warn, info, debug or trace") } // RegisterNotificationFlags that are used by watchtower to send notifications @@ -374,6 +378,7 @@ func SetDefaults() { viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT", 25) viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG", "") viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower") + viper.SetDefault("WATCHTOWER_LOG_LEVEL", "info") } // EnvConfig translates the command-line options into environment variables @@ -561,6 +566,23 @@ func ProcessFlagAliases(flags *pflag.FlagSet) { interval, _ := flags.GetInt(`interval`) flags.Set(`schedule`, fmt.Sprintf(`@every %ds`, interval)) } + + if flagIsEnabled(flags, `debug`) { + flags.Set(`log-level`, `debug`) + } + + if flagIsEnabled(flags, `trace`) { + flags.Set(`log-level`, `trace`) + } + +} + +func flagIsEnabled(flags *pflag.FlagSet, name string) bool { + value, err := flags.GetBool(name) + if err != nil { + log.Fatalf(`The flag %q is not defined`, name) + } + return value } func appendFlagValue(flags *pflag.FlagSet, name string, values ...string) error { diff --git a/internal/flags/flags_test.go b/internal/flags/flags_test.go index c7f0b80..ca6f4ae 100644 --- a/internal/flags/flags_test.go +++ b/internal/flags/flags_test.go @@ -129,11 +129,6 @@ func TestIsFile(t *testing.T) { assert.True(t, isFile(os.Args[0]), "the currently running binary path should always be considered a file") } -func TestReadFlags(t *testing.T) { - logrus.StandardLogger().ExitFunc = func(_ int) { t.FailNow() } - -} - func TestProcessFlagAliases(t *testing.T) { logrus.StandardLogger().ExitFunc = func(_ int) { t.FailNow() } cmd := new(cobra.Command) @@ -145,6 +140,7 @@ func TestProcessFlagAliases(t *testing.T) { require.NoError(t, cmd.ParseFlags([]string{ `--porcelain`, `v1`, `--interval`, `10`, + `--trace`, })) flags := cmd.Flags() ProcessFlagAliases(flags) @@ -163,6 +159,28 @@ func TestProcessFlagAliases(t *testing.T) { sched, _ := flags.GetString(`schedule`) assert.Equal(t, `@every 10s`, sched) + + logLevel, _ := flags.GetString(`log-level`) + assert.Equal(t, `trace`, logLevel) +} + +func TestProcessFlagAliasesLogLevelFromEnvironment(t *testing.T) { + cmd := new(cobra.Command) + err := os.Setenv("WATCHTOWER_DEBUG", `true`) + require.NoError(t, err) + defer os.Unsetenv("WATCHTOWER_DEBUG") + + SetDefaults() + RegisterDockerFlags(cmd) + RegisterSystemFlags(cmd) + RegisterNotificationFlags(cmd) + + require.NoError(t, cmd.ParseFlags([]string{})) + flags := cmd.Flags() + ProcessFlagAliases(flags) + + logLevel, _ := flags.GetString(`log-level`) + assert.Equal(t, `debug`, logLevel) } func TestProcessFlagAliasesSchedAndInterval(t *testing.T) {