@@ -27,6 +27,8 @@ import (
27
27
"time"
28
28
29
29
"github.com/go-logr/logr"
30
+ uzap "go.uber.org/zap"
31
+ "go.uber.org/zap/zapcore"
30
32
"helm.sh/helm/v3/pkg/getter"
31
33
"k8s.io/apimachinery/pkg/runtime"
32
34
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -66,6 +68,7 @@ func main() {
66
68
storagePath string
67
69
storageAddr string
68
70
concurrent int
71
+ logLevel string
69
72
logJSON bool
70
73
)
71
74
@@ -77,11 +80,12 @@ func main() {
77
80
flag .StringVar (& storagePath , "storage-path" , envOrDefault ("STORAGE_PATH" , "" ), "The local storage path." )
78
81
flag .StringVar (& storageAddr , "storage-addr" , envOrDefault ("STORAGE_ADDR" , ":9090" ), "The address the static file server binds to." )
79
82
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." )
80
84
flag .BoolVar (& logJSON , "log-json" , false , "Set logging to JSON format." )
81
85
82
86
flag .Parse ()
83
87
84
- ctrl .SetLogger (zap . New ( zap . UseDevMode ( ! logJSON ) ))
88
+ ctrl .SetLogger (newLogger ( logLevel , logJSON ))
85
89
86
90
var eventRecorder * recorder.EventRecorder
87
91
if eventsAddr != "" {
@@ -160,6 +164,32 @@ func main() {
160
164
}
161
165
}
162
166
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
+
163
193
func startFileServer (path string , address string , l logr.Logger ) {
164
194
fs := http .FileServer (http .Dir (path ))
165
195
http .Handle ("/" , fs )
0 commit comments