Skip to content

Commit 02c3a24

Browse files
committed
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.
1 parent 829a3c7 commit 02c3a24

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

config/manager/manager.yaml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,26 @@ spec:
2222
labels:
2323
control-plane: controller-manager
2424
spec:
25+
terminationGracePeriodSeconds: 10
2526
containers:
26-
- command:
27-
- /manager
28-
args:
29-
- --enable-leader-election
27+
- name: manager
3028
image: controller:latest
31-
name: manager
29+
ports:
30+
- containerPort: 8080
31+
name: http-prom
32+
args:
33+
- --enable-leader-election
34+
- --log-level=debug
35+
- --log-json
36+
livenessProbe:
37+
httpGet:
38+
port: http-prom
39+
path: /metrics
3240
resources:
3341
limits:
34-
cpu: 100m
35-
memory: 30Mi
42+
cpu: 1000m
43+
memory: 500Mi
3644
requests:
3745
cpu: 100m
38-
memory: 20Mi
39-
terminationGracePeriodSeconds: 10
46+
memory: 32Mi
47+

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/fluxcd/pkg v0.0.3
77
github.com/fluxcd/source-controller v0.0.2
88
github.com/go-logr/logr v0.1.0
9+
go.uber.org/zap v1.10.0
910
k8s.io/apimachinery v0.18.4
1011
k8s.io/client-go v0.18.4
1112
sigs.k8s.io/controller-runtime v0.6.0

main.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ import (
2020
"flag"
2121
"os"
2222

23-
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
24-
23+
"github.com/go-logr/logr"
24+
uzap "go.uber.org/zap"
25+
"go.uber.org/zap/zapcore"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2728
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2829
ctrl "sigs.k8s.io/controller-runtime"
2930
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3031

32+
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
3133
"github.com/stefanprodan/source-watcher/controllers"
3234
// +kubebuilder:scaffold:imports
3335
)
@@ -48,17 +50,19 @@ func main() {
4850
var (
4951
metricsAddr string
5052
enableLeaderElection bool
53+
logLevel string
5154
logJSON bool
5255
)
5356

5457
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
5558
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
5659
"Enable leader election for controller manager. "+
5760
"Enabling this will ensure there is only one active controller manager.")
61+
flag.StringVar(&logLevel, "log-level", "info", "Set logging level. Can be debug, info or error.")
5862
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
5963
flag.Parse()
6064

61-
ctrl.SetLogger(zap.New(zap.UseDevMode(!logJSON)))
65+
ctrl.SetLogger(newLogger(logLevel, logJSON))
6266

6367
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
6468
Scheme: scheme,
@@ -89,3 +93,29 @@ func main() {
8993
os.Exit(1)
9094
}
9195
}
96+
97+
// newLogger returns a logger configured for dev or production use.
98+
// For production the log format is JSON, the timestamps format is ISO8601
99+
// and stack traces are logged when the level is set to debug.
100+
func newLogger(level string, production bool) logr.Logger {
101+
if !production {
102+
return zap.New(zap.UseDevMode(true))
103+
}
104+
105+
encCfg := uzap.NewProductionEncoderConfig()
106+
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
107+
encoder := zap.Encoder(zapcore.NewJSONEncoder(encCfg))
108+
109+
logLevel := zap.Level(zapcore.InfoLevel)
110+
stacktraceLevel := zap.StacktraceLevel(zapcore.PanicLevel)
111+
112+
switch level {
113+
case "debug":
114+
logLevel = zap.Level(zapcore.DebugLevel)
115+
stacktraceLevel = zap.StacktraceLevel(zapcore.ErrorLevel)
116+
case "error":
117+
logLevel = zap.Level(zapcore.ErrorLevel)
118+
}
119+
120+
return zap.New(encoder, logLevel, stacktraceLevel)
121+
}

0 commit comments

Comments
 (0)