feat: allow log level to be set to any level (#1345)

Co-authored-by: nils måsén <nils@piksel.se>
pull/1415/head
Matthew McNeely 2 years ago committed by GitHub
parent 230312fb50
commit 0fddbfb7ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -87,11 +87,11 @@ func PreRun(cmd *cobra.Command, _ []string) {
}) })
} }
if enabled, _ := f.GetBool("debug"); enabled { rawLogLevel, _ := f.GetString(`log-level`)
log.SetLevel(log.DebugLevel) if logLevel, err := log.ParseLevel(rawLogLevel); err != nil {
} log.Fatalf("Invalid log level: %s", err.Error())
if enabled, _ := f.GetBool("trace"); enabled { } else {
log.SetLevel(log.TraceLevel) log.SetLevel(logLevel)
} }
scheduleSpec, _ = f.GetString("schedule") scheduleSpec, _ = f.GetString("schedule")

@ -71,6 +71,10 @@ Environment Variable: WATCHTOWER_REMOVE_VOLUMES
## Debug ## Debug
Enable debug mode with verbose logging. 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 ```text
Argument: --debug, -d Argument: --debug, -d
Environment Variable: WATCHTOWER_DEBUG Environment Variable: WATCHTOWER_DEBUG
@ -81,6 +85,10 @@ Environment Variable: WATCHTOWER_DEBUG
## Trace ## Trace
Enable trace mode with very verbose logging. Caution: exposes credentials! 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 ```text
Argument: --trace Argument: --trace
Environment Variable: WATCHTOWER_TRACE Environment Variable: WATCHTOWER_TRACE
@ -88,6 +96,17 @@ Environment Variable: WATCHTOWER_TRACE
Default: false 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 ## ANSI colors
Disable ANSI color escape codes in log output. 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 Environment Variable: WATCHTOWER_WARN_ON_HEAD_FAILURE
Possible values: always, auto, never Possible values: always, auto, never
Default: auto Default: auto
``` ```

@ -182,6 +182,10 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
viper.GetString("WATCHTOWER_PORCELAIN"), viper.GetString("WATCHTOWER_PORCELAIN"),
`Write session results to stdout using a stable versioned format. Supported values: "v1"`) `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 // 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_SERVER_PORT", 25)
viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG", "") viper.SetDefault("WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG", "")
viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower") viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower")
viper.SetDefault("WATCHTOWER_LOG_LEVEL", "info")
} }
// EnvConfig translates the command-line options into environment variables // EnvConfig translates the command-line options into environment variables
@ -561,6 +566,23 @@ func ProcessFlagAliases(flags *pflag.FlagSet) {
interval, _ := flags.GetInt(`interval`) interval, _ := flags.GetInt(`interval`)
flags.Set(`schedule`, fmt.Sprintf(`@every %ds`, 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 { func appendFlagValue(flags *pflag.FlagSet, name string, values ...string) error {

@ -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") 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) { func TestProcessFlagAliases(t *testing.T) {
logrus.StandardLogger().ExitFunc = func(_ int) { t.FailNow() } logrus.StandardLogger().ExitFunc = func(_ int) { t.FailNow() }
cmd := new(cobra.Command) cmd := new(cobra.Command)
@ -145,6 +140,7 @@ func TestProcessFlagAliases(t *testing.T) {
require.NoError(t, cmd.ParseFlags([]string{ require.NoError(t, cmd.ParseFlags([]string{
`--porcelain`, `v1`, `--porcelain`, `v1`,
`--interval`, `10`, `--interval`, `10`,
`--trace`,
})) }))
flags := cmd.Flags() flags := cmd.Flags()
ProcessFlagAliases(flags) ProcessFlagAliases(flags)
@ -163,6 +159,28 @@ func TestProcessFlagAliases(t *testing.T) {
sched, _ := flags.GetString(`schedule`) sched, _ := flags.GetString(`schedule`)
assert.Equal(t, `@every 10s`, sched) 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) { func TestProcessFlagAliasesSchedAndInterval(t *testing.T) {

Loading…
Cancel
Save