@@ -40,6 +40,10 @@ package klog
40
40
// >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pod status updated" pod="kube-system/kubedns" status="ready"
41
41
func InfoS (msg string , keysAndValues ...interface {})
42
42
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
+
43
47
// ErrorS structured logs to the ERROR, WARNING, and INFO logs.
44
48
// the err argument used as "err" field of log line.
45
49
// The msg argument used to add constant description to the log line.
@@ -51,6 +55,10 @@ func InfoS(msg string, keysAndValues ...interface{})
51
55
// >> E1025 00:15:15.525108 1 controller_utils.go:114] "Failed to update pod status" err="timeout"
52
56
func ErrorS (err error , msg string , keysAndValues ...interface {})
53
57
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
+
54
62
// KObj is used to create ObjectRef when logging information about Kubernetes objects
55
63
// Examples:
56
64
// >> 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
95
103
minimal design from [ logr] thus there is no one-to-one mapping.
96
104
97
105
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 `
99
108
* ` 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] )
106
115
107
116
[ see below ] : #replacing-fatal-calls
108
117
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
-
142
118
### Using ErrorS
143
119
144
120
With ` klog ` structured logging borrowing the interface from [ logr] it also inherits it's differences in semantic of
0 commit comments