Skip to content

Commit 25ca964

Browse files
Support setting depth to structured logging
Signed-off-by: JunYang <[email protected]>
1 parent fc4656b commit 25ca964

File tree

1 file changed

+16
-40
lines changed

1 file changed

+16
-40
lines changed

contributors/devel/sig-instrumentation/migration-to-structured-logging.md

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ package klog
4040
// >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pod status updated" pod="kube-system/kubedns" status="ready"
4141
func InfoS(msg string, keysAndValues ...interface{})
4242

43+
// InfoSDepth acts as InfoS but uses depth to determine which call frame to log.
44+
// InfoSDepth(0, "msg") is the same as InfoS("msg").
45+
func InfoSDepth(depth int, msg string, keysAndValues ...interface{})
46+
4347
// ErrorS structured logs to the ERROR, WARNING, and INFO logs.
4448
// the err argument used as "err" field of log line.
4549
// The msg argument used to add constant description to the log line.
@@ -51,6 +55,10 @@ func InfoS(msg string, keysAndValues ...interface{})
5155
// >> E1025 00:15:15.525108 1 controller_utils.go:114] "Failed to update pod status" err="timeout"
5256
func ErrorS(err error, msg string, keysAndValues ...interface{})
5357

58+
// ErrorSDepth acts as ErrorS but uses depth to determine which call frame to log.
59+
// ErrorSDepth(0, "msg") is the same as ErrorS("msg").
60+
func ErrorSDepth(depth int, err error, msg string, keysAndValues ...interface{})
61+
5462
// KObj is used to create ObjectRef when logging information about Kubernetes objects
5563
// Examples:
5664
// >> klog.InfoS("Pod status updated", "pod", klog.KObj(pod), "status", "ready")
@@ -95,50 +103,18 @@ Structured logging functions follow a different logging interface design than ot
95103
minimal design from [logr] thus there is no one-to-one mapping.
96104

97105
Simplified mapping between functions:
98-
* `klog.Infof`, `klog.Info`, `klog.Infoln`, `klog.InfoDepth` -> `klog.InfoS`
106+
* `klog.Infof`, `klog.Info`, `klog.Infoln` -> `klog.InfoS`
107+
* `klog.InfoDepth` -> `klog.InfoSDepth`
99108
* `klog.V(N).Infof`, `klog.V(N).Info`, `klog.V(N).Infoln` -> `klog.V(N).InfoS`
100-
* `klog.Warning`, `klog.Warningf`, `klog.Warningln`, `klog.WarningDepth` -> `klog.InfoS`
101-
* `klog.V(N).Warning`, `klog.V(N).Warningf`, `klog.V(N).Warningln`, `klog.V(N).WarningDepth` -> `klog.V(N).InfoS`
102-
* `klog.Error`, `klog.Errorf`, `klog.Errorln`, `klog.ErrorDepth` -> `klog.ErrorS`
103-
* `klog.V(N).Error`, `klog.V(N).Errorf`, `klog.V(N).Errorln`, `klog.V(N).ErrorDepth` -> `klog.ErrorS`
104-
* `klog.Fatal`, `klog.Fatalf`, `klog.Fatalln`, `klog.FatalDepth` -> `klog.ErrorS` followed by `os.Exit(1)` ([see below])
105-
* `klog.V(N).Fatal`, `klog.V(N).Fatalf`, `klog.V(N).Fatalln`, `klog.V(N).FatalDepth` -> `klog.ErrorS` followed by `os.Exit(1)` ([see below])
109+
* `klog.Warning`, `klog.Warningf`, `klog.Warningln` -> `klog.InfoS`
110+
* `klog.WarningDepth` -> `klog.InfoSDepth`
111+
* `klog.Error`, `klog.Errorf`, `klog.Errorln` -> `klog.ErrorS`
112+
* `klog.ErrorDepth` -> `klog.ErrorSDepth`
113+
* `klog.Fatal`, `klog.Fatalf`, `klog.Fatalln` -> `klog.ErrorS` followed by `os.Exit(1)` ([see below])
114+
* `klog.FatalDepth` -> `klog.ErrorDepth` followed by `os.Exit(1)` ([see below])
106115

107116
[see below]: #replacing-fatal-calls
108117

109-
### Removing Depth
110-
111-
Functions with depth (`klog.InfoDepth`, `klog.WarningDepth`, `klog.ErrorDepth`, `klog.FatalDepth`) are used to indicate
112-
that the source of the log (added as metadata in log) is different than the invocation of logging library. This is
113-
usually used when implementing logging util functions. As logr interface doesn't support depth, those functions should
114-
return logging arguments instead of calling `klog` directly.
115-
116-
For example
117-
```go
118-
func Handle(w http.ReponseWriter, r *http.Request) {
119-
logHTTPRequest(r)
120-
handle(w, r)
121-
}
122-
123-
func logHTTPRequest(r *http.Request) {
124-
klog.InfoDepth(1, "Received HTTP %s request", r.Method)
125-
}
126-
```
127-
should be replaced with
128-
```go
129-
func Handle(w http.ReponseWriter, r *http.Request) {
130-
klog.InfoS("Received HTTP request", httpRequestLog(r)...)
131-
handle(w, r)
132-
}
133-
134-
func httpRequestLog(r *http.Request) []interface{} {
135-
return []interface{}{
136-
"verb", r.Method,
137-
}
138-
}
139-
140-
```
141-
142118
### Using ErrorS
143119

144120
With `klog` structured logging borrowing the interface from [logr] it also inherits it's differences in semantic of

0 commit comments

Comments
 (0)