-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider handling values that implement the Stringer interface when logging values #302
Comments
What is |
I have a suspicion: are you using klogr with Try using |
The logger is the same as described on my previous issue: #294 We are currently using the I could observe the issue with both, though. I'll update my minimal example here (https://github.com/sbueringer/cr-klog-issue) to reproduce. I'll post an update once I've done that. |
I can reproduce it with both:
json: {"ts":1645545586459.258,"caller":"cr-klog-issue/main.go:93","msg":"controller/pod: Test log","v":0,"reconciler group":"","reconciler kind":"Pod","name":"capd-controller-manager-7dfb468fdc-74fps","namespace":"capd-system","pod":{"name":"pod-1","namespace":"default"}}
{"ts":1645545586459.389,"caller":"cr-klog-issue/main.go:94","msg":"controller/pod: Test log with .String()","v":0,"reconciler group":"","reconciler kind":"Pod","name":"capd-controller-manager-7dfb468fdc-74fps","namespace":"capd-system","pod":"default/pod-1"} text: I0222 17:00:39.171014 33977 main.go:93] controller/pod "msg"="Test log" "name"="kube-proxy-45dqb" "namespace"="kube-system" "reconciler group"="" "reconciler kind"="Pod" "pod"={"name":"pod-1","namespace":"default"}
I0222 17:00:39.171073 33977 main.go:94] controller/pod "msg"="Test log with .String()" "name"="kube-proxy-45dqb" "namespace"="kube-system" "reconciler group"="" "reconciler kind"="Pod" "pod"="default/pod-1"
EDIT: Upgraded to latest commit on klog main, but same result. |
If I see correctly:
|
This line is what I suspected: You end up using the klogr output format, which is yet another way to format things. We kept it for backward-compatiblity and also couldn't change the default for When I use
This is what you want, right? Here's the patch: diff --git a/main.go b/main.go
index 8b4203e..64f9c7a 100644
--- a/main.go
+++ b/main.go
@@ -48,14 +48,10 @@ func main() {
os.Exit(1)
}
- // The JSON log format requires the Klog format in klog, otherwise log lines
- // are serialized twice, e.g.:
- // { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"}
- if logOptions.Config.Format == logs.JSONLogFormat {
- ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
- } else {
- ctrl.SetLogger(klogr.New())
- }
+ // We log like this:
+ // klogr (FormatKlog) -> klog --> zapr (JSON format)
+ // \-> klog text format
+ ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme, |
Ah thx for the background information. Up until now, I only switched to I think it makes sense to use it for both logging formats consistently and looks like I have to for correct formatting. Just took another look at the KEP and realized that with JSON format the rendered JSON object is the expected output (https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/1602-structured-logging#serialization-strategy). I missed that before. I thought it should be P.S. Overall our goal is only to align ClusterAPI to the structured logging KEP. But as I now realized, that can be done by just specifying the right klog format. |
It doesn't provide any advantages over the other formats and is worse:
You might also want to look at kubernetes/enhancements#3222. I think your current approach will continue to work, but there is an opportunity to simplify logging by using |
Thank you very much! I assume this PR implements the KEP: #296. Tried it for my use case and it looks good overall. Just fyi. It looks like with JSON the level is currently not preserved. I think it get's lost here: Lines 38 to 44 in 56f831b
{"ts":1645610830835.741,"caller":"cr-klog-issue/main.go:95","msg":"controller/pod: Verbosity 0","v":0,"reconciler group":"","reconciler kind":"Pod","name":"coredns-558bd4d5db-nsm7l","namespace":"kube-system"}
{"ts":1645610830835.762,"caller":"cr-klog-issue/main.go:96","msg":"controller/pod: Verbosity 1","v":0,"reconciler group":"","reconciler kind":"Pod","name":"coredns-558bd4d5db-nsm7l","namespace":"kube-system"} (updated https://github.com/sbueringer/cr-klog-issue accordingly) (This issue can be closed as it's already possible today / just a matter of using the right klog format) |
Thanks for testing, I'll update the PR.
/close |
@pohly: Closing this issue. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
@pohly many thanks for the quick and detailed answers, really appreaciated! |
Yup absolutely, thank you very much! |
You are more than welcome to join us in the structured logging working group 😄 |
/kind feature
Context
When using klog today values are formatted like this: (as far as I'm aware)
This means the following log statement:
Leads to ~ this log:
The following log statement will lead to a correct log:
log:
Describe the solution you'd like
I think it would be great if the
.String()
call is not necessary, so it cannot be forgotten.To make this possible klog would have to treat either
klog.ObjectRef
or values implementingfmt.Stringer
differently (i.e. calling theirString()
func).Anything else you would like to add:
[Miscellaneous information that will assist in solving the issue.]
The text was updated successfully, but these errors were encountered: