@@ -27,6 +27,8 @@ import (
2727 "time"
2828
2929 "github.com/go-logr/logr"
30+ uzap "go.uber.org/zap"
31+ "go.uber.org/zap/zapcore"
3032 "helm.sh/helm/v3/pkg/getter"
3133 "k8s.io/apimachinery/pkg/runtime"
3234 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -66,6 +68,7 @@ func main() {
6668 storagePath string
6769 storageAddr string
6870 concurrent int
71+ logLevel string
6972 logJSON bool
7073 )
7174
@@ -77,11 +80,12 @@ func main() {
7780 flag .StringVar (& storagePath , "storage-path" , envOrDefault ("STORAGE_PATH" , "" ), "The local storage path." )
7881 flag .StringVar (& storageAddr , "storage-addr" , envOrDefault ("STORAGE_ADDR" , ":9090" ), "The address the static file server binds to." )
7982 flag .IntVar (& concurrent , "concurrent" , 2 , "The number of concurrent reconciles per controller." )
83+ flag .StringVar (& logLevel , "log-level" , "info" , "Set logging level. Can be debug, info or error." )
8084 flag .BoolVar (& logJSON , "log-json" , false , "Set logging to JSON format." )
8185
8286 flag .Parse ()
8387
84- ctrl .SetLogger (zap . New ( zap . UseDevMode ( ! logJSON ) ))
88+ ctrl .SetLogger (newLogger ( logLevel , logJSON ))
8589
8690 var eventRecorder * recorder.EventRecorder
8791 if eventsAddr != "" {
@@ -160,6 +164,32 @@ func main() {
160164 }
161165}
162166
167+ // newLogger returns a logger configured for dev or production use.
168+ // For production the log format is JSON, the timestamps format is ISO8601
169+ // and stack traces are logged when the level is set to debug.
170+ func newLogger (level string , production bool ) logr.Logger {
171+ if ! production {
172+ return zap .New (zap .UseDevMode (true ))
173+ }
174+
175+ encCfg := uzap .NewProductionEncoderConfig ()
176+ encCfg .EncodeTime = zapcore .ISO8601TimeEncoder
177+ encoder := zap .Encoder (zapcore .NewJSONEncoder (encCfg ))
178+
179+ logLevel := zap .Level (zapcore .InfoLevel )
180+ stacktraceLevel := zap .StacktraceLevel (zapcore .PanicLevel )
181+
182+ switch level {
183+ case "debug" :
184+ logLevel = zap .Level (zapcore .DebugLevel )
185+ stacktraceLevel = zap .StacktraceLevel (zapcore .ErrorLevel )
186+ case "error" :
187+ logLevel = zap .Level (zapcore .ErrorLevel )
188+ }
189+
190+ return zap .New (encoder , logLevel , stacktraceLevel )
191+ }
192+
163193func startFileServer (path string , address string , l logr.Logger ) {
164194 fs := http .FileServer (http .Dir (path ))
165195 http .Handle ("/" , fs )
0 commit comments