Skip to content

Commit

Permalink
feat: allow log level to be set to any level (#1345)
Browse files Browse the repository at this point in the history
Co-authored-by: nils måsén <nils@piksel.se>
  • Loading branch information
matthewmcneely and piksel committed Sep 17, 2022
1 parent 230312f commit 0fddbfb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 20 additions & 1 deletion docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -81,13 +85,28 @@ 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
Type: Boolean
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.

Expand Down Expand Up @@ -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
```
```
22 changes: 22 additions & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 23 additions & 5 deletions internal/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 0fddbfb

Please sign in to comment.