diff --git a/staging/src/k8s.io/component-base/logs/example/README.md b/staging/src/k8s.io/component-base/logs/example/README.md index 3159fd87cc9..a2a3a295115 100644 --- a/staging/src/k8s.io/component-base/logs/example/README.md +++ b/staging/src/k8s.io/component-base/logs/example/README.md @@ -23,9 +23,7 @@ I0329 11:36:38.734575 99095 logger.go:76] Log using Infof, key: value I0329 11:36:38.734592 99095 logger.go:77] "Log using InfoS" key="value" E0329 11:36:38.734604 99095 logger.go:79] Log using Errorf, err: fail E0329 11:36:38.734619 99095 logger.go:80] "Log using ErrorS" err="fail" -I0329 11:36:38.734631 99095 logger.go:82] Log with sensitive key, data: {"secret"} I0329 11:36:38.734653 99095 logger.go:87] "Now the default logger is set, but using the one from the context is still better." -I0329 11:36:38.734674 99095 logger.go:90] "Log sensitive data through context" data={Key:secret} I0329 11:36:38.734693 99095 logger.go:94] "runtime" duration="1m0s" I0329 11:36:38.734710 99095 logger.go:95] "another runtime" duration="1m0s" ``` @@ -46,9 +44,7 @@ This is other output via stderr. {"ts":1648546681782.9392,"caller":"cmd/logger.go:77","msg":"Log using InfoS","v":0,"key":"value"} {"ts":1648546681782.9763,"caller":"cmd/logger.go:79","msg":"Log using Errorf, err: fail\n"} {"ts":1648546681782.9915,"caller":"cmd/logger.go:80","msg":"Log using ErrorS","err":"fail"} -{"ts":1648546681783.0208,"caller":"cmd/logger.go:82","msg":"Log with sensitive key, data: {\"secret\"}\n","v":0} {"ts":1648546681783.0364,"caller":"cmd/logger.go:87","msg":"Now the default logger is set, but using the one from the context is still better.","v":0} -{"ts":1648546681783.0552,"caller":"cmd/logger.go:90","msg":"Log sensitive data through context","v":0,"data":{"key":"secret"}} {"ts":1648546681783.1091,"caller":"cmd/logger.go:94","msg":"runtime","v":0,"duration":"1m0s"} {"ts":1648546681783.1257,"caller":"cmd/logger.go:95","msg":"another runtime","v":0,"duration":"1h0m0s","duration":"1m0s"} ``` @@ -68,10 +64,8 @@ I0329 11:38:23.145944 100190 logger.go:76] Log using Infof, key: value I0329 11:38:23.145961 100190 logger.go:77] "Log using InfoS" key="value" E0329 11:38:23.145973 100190 logger.go:79] Log using Errorf, err: fail E0329 11:38:23.145989 100190 logger.go:80] "Log using ErrorS" err="fail" -I0329 11:38:23.146000 100190 logger.go:82] Log with sensitive key, data: {"secret"} I0329 11:38:23.146017 100190 logger.go:83] Log less important message I0329 11:38:23.146034 100190 logger.go:87] "Now the default logger is set, but using the one from the context is still better." -I0329 11:38:23.146055 100190 logger.go:90] "Log sensitive data through context" data={Key:secret} I0329 11:38:23.146074 100190 logger.go:94] "runtime" duration="1m0s" I0329 11:38:23.146091 100190 logger.go:95] "another runtime" duration="1m0s" ``` @@ -103,9 +97,7 @@ I0329 11:47:36.830715 101057 logger.go:76] Log using Infof, key: value I0329 11:47:36.830731 101057 logger.go:77] "Log using InfoS" key="value" E0329 11:47:36.830745 101057 logger.go:79] Log using Errorf, err: fail E0329 11:47:36.830760 101057 logger.go:80] "Log using ErrorS" err="fail" -I0329 11:47:36.830772 101057 logger.go:82] Log with sensitive key, data: {"secret"} I0329 11:47:36.830795 101057 logger.go:87] "Now the default logger is set, but using the one from the context is still better." -I0329 11:47:36.830818 101057 logger.go:90] "example: Log sensitive data through context" foo="bar" data={Key:secret} I0329 11:47:36.830841 101057 logger.go:94] "example/myname: runtime" foo="bar" duration="1m0s" I0329 11:47:36.830859 101057 logger.go:95] "example: another runtime" foo="bar" duration="1m0s" ``` diff --git a/staging/src/k8s.io/component-base/logs/example/example.go b/staging/src/k8s.io/component-base/logs/example/example.go index b2d2ab0d2e9..fc6ce68f62a 100644 --- a/staging/src/k8s.io/component-base/logs/example/example.go +++ b/staging/src/k8s.io/component-base/logs/example/example.go @@ -40,8 +40,6 @@ func Run(ctx context.Context) { err := errors.New("fail") klog.Errorf("Log using Errorf, err: %v", err) klog.ErrorS(err, "Log using ErrorS") - data := SensitiveData{Key: "secret"} - klog.Infof("Log with sensitive key, data: %q", data) klog.V(1).Info("Log less important message") // This is the fallback that can be used if neither logger nor context @@ -49,14 +47,9 @@ func Run(ctx context.Context) { klog.TODO().Info("Now the default logger is set, but using the one from the context is still better.") logger := klog.FromContext(ctx) - logger.Info("Log sensitive data through context", "data", data) // This intentionally uses the same key/value multiple times. Only the // second example could be detected via static code analysis. klog.LoggerWithValues(klog.LoggerWithName(logger, "myname"), "duration", time.Hour).Info("runtime", "duration", time.Minute) logger.Info("another runtime", "duration", time.Hour, "duration", time.Minute) } - -type SensitiveData struct { - Key string `json:"key" datapolicy:"secret-key"` -}