Skip to content

Commit dd9563b

Browse files
authored
Merge pull request #84 from fluxcd/enhancement/logging
Setup production logging
2 parents bc46153 + d988824 commit dd9563b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

config/manager/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ spec:
3333
args:
3434
- --enable-leader-election
3535
- --storage-path=/data
36+
- --log-level=debug
3637
- --log-json
3738
env:
3839
- name: RUNTIME_NAMESPACE

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/onsi/ginkgo v1.11.0
1212
github.com/onsi/gomega v1.8.1
1313
github.com/sosedoff/gitkit v0.2.1-0.20191202022816-7182d43c6254
14+
go.uber.org/zap v1.10.0
1415
helm.sh/helm/v3 v3.2.4
1516
k8s.io/api v0.18.4
1617
k8s.io/apimachinery v0.18.4

main.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
163193
func startFileServer(path string, address string, l logr.Logger) {
164194
fs := http.FileServer(http.Dir(path))
165195
http.Handle("/", fs)

0 commit comments

Comments
 (0)