Skip to content

Commit

Permalink
add json log encoder (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
clamoriniere committed Oct 4, 2020
1 parent 783987c commit 517ed87
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func main() {

// Custom flags
var printVersion, pprofActive bool
var logEncoder string
flag.StringVar(&logEncoder, "logEncoder", "json", "log encoding ('json' or 'console')")
logLevel := zap.LevelFlag("loglevel", zapcore.InfoLevel, "Set log level")
flag.BoolVar(&printVersion, "version", false, "Print version and exit")
flag.BoolVar(&pprofActive, "pprof", false, "Enable pprof endpoint")
Expand All @@ -69,7 +71,10 @@ func main() {
flag.Parse()

// Logging setup
customSetupLogging(*logLevel)
if err := customSetupLogging(*logLevel, logEncoder); err != nil {
setupLog.Error(err, "unable to setup the logger")
os.Exit(1)
}

// Print version information
if printVersion {
Expand Down Expand Up @@ -146,12 +151,24 @@ func customSetupEnvironment(mgr manager.Manager) {
}
}

func customSetupLogging(logLevel zapcore.Level) {
func customSetupLogging(logLevel zapcore.Level, logEncoder string) error {
var encoder zapcore.Encoder
switch logEncoder {
case "console":
encoder = zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig())
case "json":
encoder = zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
default:
return fmt.Errorf("unknow log encoder: %s", logEncoder)
}

ctrl.SetLogger(ctrlzap.New(
ctrlzap.Encoder(zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig())),
ctrlzap.Encoder(encoder),
ctrlzap.Level(logLevel),
ctrlzap.StacktraceLevel(zapcore.PanicLevel)),
)

return nil
}

func customSetupHealthChecks(mgr manager.Manager) {
Expand Down

0 comments on commit 517ed87

Please sign in to comment.