diff --git a/cmd/root.go b/cmd/root.go index dbb7e89..55221cc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,17 +28,18 @@ import ( ) var ( - client container.Client - scheduleSpec string - cleanup bool - noRestart bool - monitorOnly bool - enableLabel bool - notifier t.Notifier - timeout time.Duration - lifecycleHooks bool - rollingRestart bool - scope string + client container.Client + scheduleSpec string + cleanup bool + noRestart bool + monitorOnly bool + enableLabel bool + enableJsonFormatter bool + notifier t.Notifier + timeout time.Duration + lifecycleHooks bool + rollingRestart bool + scope string ) var rootCmd = NewRootCommand() @@ -96,6 +97,11 @@ func PreRun(cmd *cobra.Command, _ []string) { log.SetLevel(logLevel) } + enableJsonFormatter, _ = f.GetBool("json-logging") + if enableJsonFormatter { + log.SetFormatter(&log.JSONFormatter{}) + } + scheduleSpec, _ = f.GetString("schedule") flags.GetSecretsFromFiles(cmd) diff --git a/docs/arguments.md b/docs/arguments.md index d0e46ae..0b24c36 100644 --- a/docs/arguments.md +++ b/docs/arguments.md @@ -107,6 +107,17 @@ Environment Variable: WATCHTOWER_LOG_LEVEL Default: info ``` +## JSON Logging + +Enables the JSON log formatter for logging, changing the log output to JSON format. By default, the log output follows the standard text-based format. + +```text + Argument: --json-logging +Environment Variable: WATCHTOWER_JSON_LOGGING + Type: Boolean + Default: false +``` + ## ANSI colors Disable ANSI color escape codes in log output. diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 5428b95..12665fe 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -86,6 +86,12 @@ func RegisterSystemFlags(rootCmd *cobra.Command) { viper.GetBool("WATCHTOWER_LABEL_ENABLE"), "Watch containers where the com.centurylinklabs.watchtower.enable label is true") + flags.BoolP( + "json-logging", + "j", + viper.GetBool("WATCHTOWER_JSON_LOGGING"), + "Enables the JSON log formatter for logging") + flags.BoolP( "debug", "d", diff --git a/internal/flags/flags_test.go b/internal/flags/flags_test.go index ca6f4ae..d102e07 100644 --- a/internal/flags/flags_test.go +++ b/internal/flags/flags_test.go @@ -183,6 +183,28 @@ func TestProcessFlagAliasesLogLevelFromEnvironment(t *testing.T) { assert.Equal(t, `debug`, logLevel) } +func TestJSONLoggingFlag(t *testing.T) { + cmd := new(cobra.Command) + + SetDefaults() + RegisterDockerFlags(cmd) + RegisterSystemFlags(cmd) + + // Ensure the default value is false + enableJsonFormatter, err := cmd.PersistentFlags().GetBool("json-logging") + require.NoError(t, err) + + assert.Equal(t, false, enableJsonFormatter) + + // Test with the argument + require.NoError(t, cmd.ParseFlags([]string{`--json-logging`})) + flags := cmd.Flags() + enableJsonFormatter, _ = flags.GetBool("json-logging") + require.NoError(t, err) + assert.Equal(t, true, enableJsonFormatter) + +} + func TestProcessFlagAliasesSchedAndInterval(t *testing.T) { logrus.StandardLogger().ExitFunc = func(_ int) { panic(`FATAL`) } cmd := new(cobra.Command)