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 {
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")

@ -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
```
```

@ -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 {

@ -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) {

Loading…
Cancel
Save