Skip to content

Commit 1867742

Browse files
feat: Add logformat flag to rollouts-controller (argoproj#1818)
Signed-off-by: Karol Szlachta <[email protected]>
1 parent 2fda86a commit 1867742

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

cmd/rollouts-controller/main.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"time"
78

89
smiclientset "github.com/servicemeshinterface/smi-sdk-go/pkg/gen/client/split/clientset/versioned"
@@ -36,14 +37,17 @@ import (
3637

3738
const (
3839
// CLIName is the name of the CLI
39-
cliName = "argo-rollouts"
40+
cliName = "argo-rollouts"
41+
jsonFormat = "json"
42+
textFormat = "text"
4043
)
4144

4245
func newCommand() *cobra.Command {
4346
var (
4447
clientConfig clientcmd.ClientConfig
4548
rolloutResyncPeriod int64
4649
logLevel string
50+
logFormat string
4751
klogLevel int
4852
metricsPort int
4953
healthzPort int
@@ -76,10 +80,9 @@ func newCommand() *cobra.Command {
7680
return nil
7781
}
7882
setLogLevel(logLevel)
79-
formatter := &log.TextFormatter{
80-
FullTimestamp: true,
83+
if logFormat != "" {
84+
log.SetFormatter(createFormatter(logFormat))
8185
}
82-
log.SetFormatter(formatter)
8386
logutil.SetKLogLevel(klogLevel)
8487
log.WithField("version", version.GetVersion()).Info("Argo Rollouts starting")
8588

@@ -215,6 +218,7 @@ func newCommand() *cobra.Command {
215218
command.Flags().Int64Var(&rolloutResyncPeriod, "rollout-resync", controller.DefaultRolloutResyncPeriod, "Time period in seconds for rollouts resync.")
216219
command.Flags().BoolVar(&namespaced, "namespaced", false, "runs controller in namespaced mode (does not require cluster RBAC)")
217220
command.Flags().StringVar(&logLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error")
221+
command.Flags().StringVar(&logFormat, "logformat", "", "Set the logging format. One of: text|json")
218222
command.Flags().IntVar(&klogLevel, "kloglevel", 0, "Set the klog logging level")
219223
command.Flags().IntVar(&metricsPort, "metricsport", controller.DefaultMetricsPort, "Set the port the metrics endpoint should be exposed over")
220224
command.Flags().IntVar(&healthzPort, "healthzPort", controller.DefaultHealthzPort, "Set the port the healthz endpoint should be exposed over")
@@ -270,6 +274,25 @@ func setLogLevel(logLevel string) {
270274
log.SetLevel(level)
271275
}
272276

277+
func createFormatter(logFormat string) log.Formatter {
278+
var formatType log.Formatter
279+
switch strings.ToLower(logFormat) {
280+
case jsonFormat:
281+
formatType = &log.JSONFormatter{}
282+
case textFormat:
283+
formatType = &log.TextFormatter{
284+
FullTimestamp: true,
285+
}
286+
default:
287+
log.Infof("Unknown format: %s. Using text logformat", logFormat)
288+
formatType = &log.TextFormatter{
289+
FullTimestamp: true,
290+
}
291+
}
292+
293+
return formatType
294+
}
295+
273296
func checkError(err error) {
274297
if err != nil {
275298
log.Fatal(err)

0 commit comments

Comments
 (0)