klog is interesting:
it implements
logr.Logger
with klogr
but also accepts
a logr.Logger
as the output.
Anyway, here's some commented code to show how the setups interact.
Passing klogr->klog->some_other_logr
looks inefficient and doesn't get you the best results anyways...
1package main
2
3import (
4 "errors"
5
6 "github.com/butonic/zerologr"
7 "k8s.io/klog/v2"
8 "k8s.io/klog/v2/klogr"
9)
10
11func main() {
12 zl := zerologr.New() // an implementation of logr.Logger
13 err := errors.New("some: error")
14
15 // global default klog
16 klog.Errorln("hello world", "foo", "bar")
17 // E0617 23:38:11.432545 83114 main.go:16] hello world foo bar
18 klog.ErrorS(err, "oopsie daisy", "foo", "bar")
19 // E0617 23:38:11.432664 83114 main.go:17] "oopsie daisy" err="some: error" foo="bar"
20
21
22 // klogr with default serialization (pass single string to klog)
23 kl := klogr.NewWithOptions(klogr.WithFormat(klogr.FormatSerialize))
24 kl = kl.WithName("app").WithName("comp1")
25 kl.Info("hello world", "foo", "bar")
26 // I0617 23:38:11.432704 83114 main.go:21] app/comp1 "msg"="hello world" "foo"="bar"
27 kl.Error(err, "oopsie daisy", "foo", "bar")
28 // E0617 23:38:11.432720 83114 main.go:22] app/comp1 "msg"="oopsie daisy" "error"="some: error" "foo"="bar"
29
30
31 // klogr passing serialization to klog
32 kl = klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))
33 kl = kl.WithName("app").WithName("comp1")
34 kl.Info("hello world", "foo", "bar")
35 // I0617 23:38:11.432736 83114 main.go:26] "app/comp1: hello world" foo="bar"
36 kl.Error(err, "oopsie daisy", "foo", "bar")
37 // E0617 23:38:11.432749 83114 main.go:27] "app/comp1: oopsie daisy" err="some: error" foo="bar"
38
39
40 // use a logr.Logger for klog output
41 // default output
42 klog.SetLogger(zl)
43 klog.Errorln("hello world", "foo", "bar")
44 // {"level":"error","time":"2021-06-17T23:38:11+02:00","message":"hello world foo bar\n"}
45 klog.ErrorS(err, "oopsie daisy", "foo", "bar")
46 // {"level":"error","error":"some: error","foo":"bar","time":"2021-06-17T23:38:11+02:00","message":"oopsie daisy"}
47
48
49 // klogr with default serialization (pass single string to klog)
50 kl = klogr.NewWithOptions(klogr.WithFormat(klogr.FormatSerialize))
51 kl = kl.WithName("app").WithName("comp1")
52 kl.Info("hello world", "foo", "bar")
53 // {"level":"info","verbosity":0,"time":"2021-06-17T23:38:11+02:00","message":"app/comp1 \"msg\"=\"hello world\" \"foo\"=\"bar\"\n"}
54 kl.Error(err, "oopsie daisy", "foo", "bar")
55 // {"level":"error","time":"2021-06-17T23:38:11+02:00","message":"app/comp1 \"msg\"=\"oopsie daisy\" \"error\"=\"some: error\" \"foo\"=\"bar\"\n"}
56
57
58 // klogr passing serialization to klog
59 kl = klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))
60 kl = kl.WithName("app").WithName("comp1")
61 kl.Info("hello world", "foo", "bar")
62 // {"level":"info","verbosity":0,"foo":"bar","time":"2021-06-17T23:38:11+02:00","message":"app/comp1: hello world"}
63 kl.Error(err, "oopsie daisy", "foo", "bar")
64 // {"level":"error","error":"some: error","foo":"bar","time":"2021-06-17T23:38:11+02:00","message":"app/comp1: oopsie daisy"}
65
66
67 // what you would get if you used the logr.Logger directly
68 zl = zl.WithName("app").WithName("comp1")
69 zl.Info("hello world", "foo", "bar")
70 // {"level":"info","verbosity":0,"name":"app/comp1","foo":"bar","time":"2021-06-17T23:52:34+02:00","message":"hello world"}
71 zl.Error(err, "oopsie daisy", "foo", "bar")
72 // {"level":"error","error":"some: error","name":"app/comp1","foo":"bar","time":"2021-06-17T23:52:34+02:00","message":"oopsie daisy"}
73}