Setup production logging
For production the log format is JSON, the timestamps format is ISO8601 and stack traces are logged when the level is set to debug.
This commit is contained in:
parent
d827b3790b
commit
3acb82fa0b
|
|
@ -39,6 +39,7 @@ spec:
|
||||||
path: /metrics
|
path: /metrics
|
||||||
args:
|
args:
|
||||||
- --enable-leader-election
|
- --enable-leader-election
|
||||||
|
- --log-level=debug
|
||||||
- --log-json
|
- --log-json
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -8,6 +8,7 @@ require (
|
||||||
github.com/go-logr/logr v0.1.0
|
github.com/go-logr/logr v0.1.0
|
||||||
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
|
||||||
|
go.uber.org/zap v1.10.0
|
||||||
k8s.io/api v0.18.4
|
k8s.io/api v0.18.4
|
||||||
k8s.io/apimachinery v0.18.4
|
k8s.io/apimachinery v0.18.4
|
||||||
k8s.io/client-go v0.18.4
|
k8s.io/client-go v0.18.4
|
||||||
|
|
|
||||||
42
main.go
42
main.go
|
|
@ -21,15 +21,19 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1"
|
"github.com/go-logr/logr"
|
||||||
"github.com/fluxcd/kustomize-controller/controllers"
|
uzap "go.uber.org/zap"
|
||||||
"github.com/fluxcd/pkg/recorder"
|
"go.uber.org/zap/zapcore"
|
||||||
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
|
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
|
||||||
ctrl "sigs.k8s.io/controller-runtime"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||||
|
|
||||||
|
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1"
|
||||||
|
"github.com/fluxcd/kustomize-controller/controllers"
|
||||||
|
"github.com/fluxcd/pkg/recorder"
|
||||||
|
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
|
||||||
// +kubebuilder:scaffold:imports
|
// +kubebuilder:scaffold:imports
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -53,6 +57,7 @@ func main() {
|
||||||
enableLeaderElection bool
|
enableLeaderElection bool
|
||||||
concurrent int
|
concurrent int
|
||||||
requeueDependency time.Duration
|
requeueDependency time.Duration
|
||||||
|
logLevel string
|
||||||
logJSON bool
|
logJSON bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -63,10 +68,11 @@ func main() {
|
||||||
"Enabling this will ensure there is only one active controller manager.")
|
"Enabling this will ensure there is only one active controller manager.")
|
||||||
flag.IntVar(&concurrent, "concurrent", 4, "The number of concurrent kustomize reconciles.")
|
flag.IntVar(&concurrent, "concurrent", 4, "The number of concurrent kustomize reconciles.")
|
||||||
flag.DurationVar(&requeueDependency, "requeue-dependency", 30*time.Second, "The interval at which failing dependencies are reevaluated.")
|
flag.DurationVar(&requeueDependency, "requeue-dependency", 30*time.Second, "The interval at which failing dependencies are reevaluated.")
|
||||||
|
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 != "" {
|
||||||
|
|
@ -120,3 +126,29 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue