Skip to content

Commit 62a5452

Browse files
authored
Add support for JSON log formatting (#1231)
Signed-off-by: Michael Pilat <[email protected]>
1 parent ded7a28 commit 62a5452

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type ImageUpdaterConfig struct {
3636
CheckInterval time.Duration
3737
ArgoClient argocd.ArgoCD
3838
LogLevel string
39+
LogFormat string
3940
KubeClient *kube.ImageUpdaterKubernetesClient
4041
MaxConcurrency int
4142
HealthPort int

cmd/run.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ func newRunCommand() *cobra.Command {
5151
return err
5252
}
5353

54+
var logFormat log.LogFormat
55+
switch cfg.LogFormat {
56+
case "text":
57+
logFormat = log.LogFormatText
58+
case "json":
59+
logFormat = log.LogFormatJSON
60+
default:
61+
return fmt.Errorf("Invalid log format '%s'", cfg.LogFormat)
62+
}
63+
log.SetLogFormat(logFormat)
64+
5465
if once {
5566
cfg.CheckInterval = 0
5667
cfg.HealthPort = 0
@@ -323,6 +334,7 @@ func newRunCommand() *cobra.Command {
323334
runCmd.Flags().BoolVar(&cfg.DryRun, "dry-run", false, "run in dry-run mode. If set to true, do not perform any changes")
324335
runCmd.Flags().DurationVar(&cfg.CheckInterval, "interval", env.GetDurationVal("IMAGE_UPDATER_INTERVAL", 2*time.Minute), "interval for how often to check for updates")
325336
runCmd.Flags().StringVar(&cfg.LogLevel, "loglevel", env.GetStringVal("IMAGE_UPDATER_LOGLEVEL", "info"), "set the loglevel to one of trace|debug|info|warn|error")
337+
runCmd.Flags().StringVar(&cfg.LogFormat, "logformat", env.GetStringVal("IMAGE_UPDATER_LOGFORMAT", "text"), "set the log format to one of text|json")
326338
runCmd.Flags().StringVar(&kubeConfig, "kubeconfig", "", "full path to kubernetes client configuration, i.e. ~/.kube/config")
327339
runCmd.Flags().IntVar(&cfg.HealthPort, "health-port", 8080, "port to start the health server on, 0 to disable")
328340
runCmd.Flags().IntVar(&cfg.MetricsPort, "metrics-port", 8081, "port to start the metrics server on, 0 to disable")

cmd/run_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestNewRunCommand(t *testing.T) {
2626
asser.Equal("false", runCmd.Flag("dry-run").Value.String())
2727
asser.Equal(env.GetDurationVal("IMAGE_UPDATER_INTERVAL", 2*time.Minute).String(), runCmd.Flag("interval").Value.String())
2828
asser.Equal(env.GetStringVal("IMAGE_UPDATER_LOGLEVEL", "info"), runCmd.Flag("loglevel").Value.String())
29+
asser.Equal(env.GetStringVal("IMAGE_UPDATER_LOGFORMAT", "text"), runCmd.Flag("logformat").Value.String())
2930
asser.Equal("", runCmd.Flag("kubeconfig").Value.String())
3031
asser.Equal("8080", runCmd.Flag("health-port").Value.String())
3132
asser.Equal("8081", runCmd.Flag("metrics-port").Value.String())

docs/install/cmd/run.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ Set the log level to *level*, where *level* can be one of `trace`, `debug`,
195195

196196
Can also be set using the *IMAGE_UPDATER_LOGLEVEL* environment variable.
197197

198+
**--logformat *format***
199+
200+
Set the log format to *format*, where *format* can be one of `text` or `json`
201+
202+
Can also be set using the *IMAGE_UPDATER_LOGFORMAT* environment variable.
203+
198204
**--match-application-label *selector***
199205

200206
Only process applications that have a valid annotation and match the given

registry-scanner/pkg/log/log.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ func SetLogLevel(logLevel string) error {
6161
return nil
6262
}
6363

64+
type LogFormat int
65+
66+
const (
67+
LogFormatText LogFormat = iota
68+
LogFormatJSON
69+
)
70+
71+
func SetLogFormat(logFormat LogFormat) {
72+
switch logFormat {
73+
case LogFormatText:
74+
logger.SetFormatter(&logrus.TextFormatter{DisableColors: disableLogColors()})
75+
case LogFormatJSON:
76+
logger.SetFormatter(&logrus.JSONFormatter{})
77+
}
78+
}
79+
6480
// WithContext is an alias for NewContext
6581
func WithContext() *LogContext {
6682
return NewContext()

registry-scanner/pkg/log/log_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package log
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"testing"
67

@@ -154,3 +155,31 @@ func Test_LogLevel(t *testing.T) {
154155
assert.Error(t, err)
155156
})
156157
}
158+
159+
func Test_LogFormatJSON(t *testing.T) {
160+
// We need tracing level
161+
Log().SetLevel(logrus.TraceLevel)
162+
163+
t.Run("Test set text log format", func(t *testing.T) {
164+
SetLogFormat(LogFormatText)
165+
out, err := fixture.CaptureStdout(func() {
166+
WithContext().AddField("foo", "bar").Infof("this is a test")
167+
})
168+
assert.NoError(t, err)
169+
assert.Contains(t, out, "foo=bar")
170+
assert.Contains(t, out, "msg=\"this is a test\"")
171+
})
172+
t.Run("Test set JSON log format", func(t *testing.T) {
173+
SetLogFormat(LogFormatJSON)
174+
out, err := fixture.CaptureStdout(func() {
175+
WithContext().AddField("foo", "bar").Warnf("this is a test")
176+
})
177+
assert.NoError(t, err)
178+
179+
var data map[string]any
180+
err = json.Unmarshal([]byte(out), &data)
181+
assert.NoError(t, err)
182+
assert.Equal(t, "bar", data["foo"])
183+
assert.Equal(t, "this is a test", data["msg"])
184+
})
185+
}

0 commit comments

Comments
 (0)