Merge pull request #84 from fluxcd/enhancement/logging
Setup production logging
This commit is contained in:
commit
dd9563ba98
|
@ -33,6 +33,7 @@ spec:
|
||||||
args:
|
args:
|
||||||
- --enable-leader-election
|
- --enable-leader-election
|
||||||
- --storage-path=/data
|
- --storage-path=/data
|
||||||
|
- --log-level=debug
|
||||||
- --log-json
|
- --log-json
|
||||||
env:
|
env:
|
||||||
- name: RUNTIME_NAMESPACE
|
- name: RUNTIME_NAMESPACE
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -11,6 +11,7 @@ require (
|
||||||
github.com/onsi/ginkgo v1.11.0
|
github.com/onsi/ginkgo v1.11.0
|
||||||
github.com/onsi/gomega v1.8.1
|
github.com/onsi/gomega v1.8.1
|
||||||
github.com/sosedoff/gitkit v0.2.1-0.20191202022816-7182d43c6254
|
github.com/sosedoff/gitkit v0.2.1-0.20191202022816-7182d43c6254
|
||||||
|
go.uber.org/zap v1.10.0
|
||||||
helm.sh/helm/v3 v3.2.4
|
helm.sh/helm/v3 v3.2.4
|
||||||
k8s.io/api v0.18.4
|
k8s.io/api v0.18.4
|
||||||
k8s.io/apimachinery v0.18.4
|
k8s.io/apimachinery v0.18.4
|
||||||
|
|
32
main.go
32
main.go
|
@ -27,6 +27,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
|
uzap "go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
"helm.sh/helm/v3/pkg/getter"
|
"helm.sh/helm/v3/pkg/getter"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
||||||
|
@ -66,6 +68,7 @@ func main() {
|
||||||
storagePath string
|
storagePath string
|
||||||
storageAddr string
|
storageAddr string
|
||||||
concurrent int
|
concurrent int
|
||||||
|
logLevel string
|
||||||
logJSON bool
|
logJSON bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -77,11 +80,12 @@ func main() {
|
||||||
flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""), "The local storage path.")
|
flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""), "The local storage path.")
|
||||||
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"), "The address the static file server binds to.")
|
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"), "The address the static file server binds to.")
|
||||||
flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.")
|
flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.")
|
||||||
|
flag.StringVar(&logLevel, "log-level", "info", "Set logging level. Can be debug, info or error.")
|
||||||
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
|
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
ctrl.SetLogger(zap.New(zap.UseDevMode(!logJSON)))
|
ctrl.SetLogger(newLogger(logLevel, logJSON))
|
||||||
|
|
||||||
var eventRecorder *recorder.EventRecorder
|
var eventRecorder *recorder.EventRecorder
|
||||||
if eventsAddr != "" {
|
if eventsAddr != "" {
|
||||||
|
@ -160,6 +164,32 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newLogger returns a logger configured for dev or production use.
|
||||||
|
// For production the log format is JSON, the timestamps format is ISO8601
|
||||||
|
// and stack traces are logged when the level is set to debug.
|
||||||
|
func newLogger(level string, production bool) logr.Logger {
|
||||||
|
if !production {
|
||||||
|
return zap.New(zap.UseDevMode(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
encCfg := uzap.NewProductionEncoderConfig()
|
||||||
|
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||||
|
encoder := zap.Encoder(zapcore.NewJSONEncoder(encCfg))
|
||||||
|
|
||||||
|
logLevel := zap.Level(zapcore.InfoLevel)
|
||||||
|
stacktraceLevel := zap.StacktraceLevel(zapcore.PanicLevel)
|
||||||
|
|
||||||
|
switch level {
|
||||||
|
case "debug":
|
||||||
|
logLevel = zap.Level(zapcore.DebugLevel)
|
||||||
|
stacktraceLevel = zap.StacktraceLevel(zapcore.ErrorLevel)
|
||||||
|
case "error":
|
||||||
|
logLevel = zap.Level(zapcore.ErrorLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
return zap.New(encoder, logLevel, stacktraceLevel)
|
||||||
|
}
|
||||||
|
|
||||||
func startFileServer(path string, address string, l logr.Logger) {
|
func startFileServer(path string, address string, l logr.Logger) {
|
||||||
fs := http.FileServer(http.Dir(path))
|
fs := http.FileServer(http.Dir(path))
|
||||||
http.Handle("/", fs)
|
http.Handle("/", fs)
|
||||||
|
|
Loading…
Reference in New Issue