/* Copyright 2022 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package test // ZaprOutputMappingDirect provides a mapping from klog output to the // corresponding zapr output when zapr is called directly. func ZaprOutputMappingDirect() map[string]string { return map[string]string{ `I output.go:] "test" akey="<&>" `: `{"caller":"test/output.go:","msg":"test","v":0,"akey":"<&>"} `, `E output.go:] "test" err="whoops" `: `{"caller":"test/output.go:","msg":"test","err":"whoops"} `, `I output.go:] "helper" akey="avalue" `: `{"caller":"test/output.go:","msg":"helper","v":0,"akey":"avalue"} `, `I output.go:] "test" akey="avalue" akey="avalue2" `: `{"caller":"test/output.go:","msg":"test","v":0,"akey":"avalue","akey":"avalue2"} `, `I output.go:] "test" logger="hello.world" akey="avalue" `: `{"logger":"hello.world","caller":"test/output.go:","msg":"test","v":0,"akey":"avalue"} `, `I output.go:] "test" X="y" duration="1m0s" A="b" `: `{"caller":"test/output.go:","msg":"test","duration":"1h0m0s","X":"y","v":0,"duration":"1m0s","A":"b"} `, `I output.go:] "test" akey9="avalue9" akey8="avalue8" akey1="avalue1" akey5="avalue5" akey4="avalue4" `: `{"caller":"test/output.go:","msg":"test","akey9":"avalue9","akey8":"avalue8","akey1":"avalue1","v":0,"akey5":"avalue5","akey4":"avalue4"} `, `I output.go:] "test" `: `{"caller":"test/output.go:","msg":"test","v":0} `, `I output.go:] "\"quoted\"" key="\"quoted value\"" `: `{"caller":"test/output.go:","msg":"\"quoted\"","v":0,"key":"\"quoted value\""} `, `I output.go:] "test" err="whoops" `: `{"caller":"test/output.go:","msg":"test","v":0,"err":"whoops"} `, `I output.go:] "test" pod="kube-system/pod-1" `: `{"caller":"test/output.go:","msg":"test","v":0,"pod":{"name":"pod-1","namespace":"kube-system"}} `, `I output.go:] "KObjs" pods=[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}] `: `{"caller":"test/output.go:","msg":"KObjs","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]} `, `I output.go:] "KObjSlice" pods=["kube-system/pod-1","kube-system/pod-2"] `: `{"caller":"test/output.go:","msg":"KObjSlice","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]} `, `I output.go:] "test" pods=null `: `{"caller":"test/output.go:","msg":"test","v":0,"pods":null} `, `I output.go:] "test" pods="" `: `{"caller":"test/output.go:","msg":"test","v":0,"pods":""} `, `I output.go:] "test" ints=[""] `: `{"caller":"test/output.go:","msg":"test","v":0,"ints":""} `, `I output.go:] "test" pods=["kube-system/pod-1",null] `: `{"caller":"test/output.go:","msg":"test","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},null]} `, `I output.go:] "test" akey="avalue" `: `{"caller":"test/output.go:","msg":"test","v":0,"akey":"avalue"} `, `I output.go:] "test" logger="me" akey="avalue" `: `{"logger":"me","caller":"test/output.go:","msg":"test","v":0,"akey":"avalue"} `, `I output.go:] "test" akey="avalue2" `: `{"caller":"test/output.go:","msg":"test","akey":"avalue","v":0,"akey":"avalue2"} `, `I output.go:] "you see me" `: `{"caller":"test/output.go:","msg":"you see me","v":9} `, `I output.go:] "test" firstKey=1 I output.go:] "test" firstKey=1 secondKey=2 I output.go:] "test" firstKey=1 I output.go:] "test" firstKey=1 secondKey=3 `: `{"caller":"test/output.go:","msg":"test","firstKey":1,"v":0} {"caller":"test/output.go:","msg":"test","firstKey":1,"secondKey":2,"v":0} {"caller":"test/output.go:","msg":"test","firstKey":1,"v":0} {"caller":"test/output.go:","msg":"test","firstKey":1,"secondKey":3,"v":0} `, `I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" anotherKeyWithoutValue="(MISSING)" I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" `: `{"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"keyWithoutValue"} {"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"anotherKeyWithoutValue"} {"caller":"test/output.go:","msg":"odd WithValues","v":0} {"caller":"test/output.go:","msg":"odd WithValues","v":0} {"caller":"test/output.go:","msg":"odd WithValues","v":0} `, `I output.go:] "odd arguments" akey="avalue" akey2="(MISSING)" `: `{"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"} {"caller":"test/output.go:","msg":"odd arguments","v":0,"akey":"avalue"} `, `I output.go:] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)" `: `{"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"basekey2"} {"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","basekey1":"basevar1","ignored key":"akey2"} {"caller":"test/output.go:","msg":"both odd","basekey1":"basevar1","v":0,"akey":"avalue"} `, `I output.go:] "marshaler nil" obj="" `: `{"caller":"test/output.go:","msg":"marshaler nil","v":0,"objError":"PANIC=value method k8s.io/klog/v2.ObjectRef.MarshalLog called using nil *ObjectRef pointer"} `, // zap replaces a panic for a nil object with . `E output.go:] "error nil" err="" `: `{"caller":"test/output.go:","msg":"error nil","err":""} `, `I output.go:] "stringer nil" stringer="" `: `{"caller":"test/output.go:","msg":"stringer nil","v":0,"stringer":""} `, `I output.go:] "stringer panic" stringer="" `: `{"caller":"test/output.go:","msg":"stringer panic","v":0,"stringerError":"PANIC=fake String panic"} `, `E output.go:] "error panic" err="" `: `{"caller":"test/output.go:","msg":"error panic","errError":"PANIC=fake Error panic"} `, `I output.go:] "marshaler panic" obj="" `: `{"caller":"test/output.go:","msg":"marshaler panic","v":0,"objError":"PANIC=fake MarshalLog panic"} `, `I output.go:] "marshaler recursion" obj={} `: `{"caller":"test/output.go:","msg":"marshaler recursion","v":0,"obj":{}} `, // klog.Info `I output.go:] "helloworld\n" `: `{"caller":"test/output.go:","msg":"helloworld","v":0} `, // klog.Infoln `I output.go:] "hello world\n" `: `{"caller":"test/output.go:","msg":"hello world","v":0} `, // klog.Error `E output.go:] "helloworld\n" `: `{"caller":"test/output.go:","msg":"helloworld"} `, // klog.Errorln `E output.go:] "hello world\n" `: `{"caller":"test/output.go:","msg":"hello world"} `, // klog.ErrorS `E output.go:] "world" err="hello" `: `{"caller":"test/output.go:","msg":"world","err":"hello"} `, // klog.InfoS `I output.go:] "hello" what="world" `: `{"caller":"test/output.go:","msg":"hello","v":0,"what":"world"} `, // klog.V(1).Info `I output.go:] "hellooneworld\n" `: `{"caller":"test/output.go:","msg":"hellooneworld","v":1} `, // klog.V(1).Infoln `I output.go:] "hello one world\n" `: `{"caller":"test/output.go:","msg":"hello one world","v":1} `, // klog.V(1).ErrorS `E output.go:] "one world" err="hello" `: `{"caller":"test/output.go:","msg":"one world","err":"hello"} `, // klog.V(1).InfoS `I output.go:] "hello" what="one world" `: `{"caller":"test/output.go:","msg":"hello","v":1,"what":"one world"} `, `I output.go:] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)" `: `{"caller":"test/output.go:","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1} {"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"} {"caller":"test/output.go:","msg":"integer keys","v":0,"akey":"avalue"} `, `I output.go:] "struct keys" {name}="value" test="other value" key="val" `: `{"caller":"test/output.go:","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}} {"caller":"test/output.go:","msg":"struct keys","v":0,"key":"val"} `, `I output.go:] "map keys" map[test:%!s(bool=true)]="test" `: `{"caller":"test/output.go:","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}} {"caller":"test/output.go:","msg":"map keys","v":0} `, `I output.go:] "Format" config=< { "Kind": "config", "RealField": 42 } > `: `{"caller":"test/output.go:","msg":"Format","v":0,"config":{"Kind":"config","RealField":42}} `, `I output.go:] "maps" s={"hello":"world"} i={"1":2,"3":4} `: `{"caller":"test/output.go:","msg":"maps","v":0,"s":{"hello":"world"},"i":{"1":2,"3":4}} `, `I output.go:] "slices" s=["hello","world"] i=[1,2,3] `: `{"caller":"test/output.go:","msg":"slices","v":0,"s":["hello","world"],"i":[1,2,3]} `, `I output.go:] "structs" s={"Name":"worker","Kind":"pod"} `: `{"caller":"test/output.go:","msg":"structs","v":0,"s":{"Name":"worker","Kind":"pod"}} `, `I output.go:] "klog.Format" s=< { "Name": "worker", "Kind": "pod" } > `: `{"caller":"test/output.go:","msg":"klog.Format","v":0,"s":{"Name":"worker","Kind":"pod"}} `, `I output.go:] "cycle" list="" `: `{"caller":"test/output.go:","msg":"cycle","v":0,"listError":"json: unsupported value: encountered a cycle via *test.myList"} `, } } // ZaprOutputMappingIndirect provides a mapping from klog output to the // corresponding zapr output when zapr is called indirectly through // klog. // // This is different from ZaprOutputMappingDirect because: // - WithName gets added to the message by Output. // - zap uses . as separator instead of / between WithName values, // here we get slashes because Output concatenates these values. // - WithValues are added to the normal key/value parameters by // Output, which puts them after "v". // - Output does that without emitting the warning that we get // from zapr. // - zap drops keys with missing values, here we get "(MISSING)". // - zap does not de-duplicate key/value pairs, here klog does that // for it. func ZaprOutputMappingIndirect() map[string]string { mapping := ZaprOutputMappingDirect() for key, value := range map[string]string{ `I output.go:] "test" logger="hello.world" akey="avalue" `: `{"caller":"test/output.go:","msg":"test","v":0,"logger":"hello.world","akey":"avalue"} `, `I output.go:] "test" logger="me" akey="avalue" `: `{"caller":"test/output.go:","msg":"test","v":0,"logger":"me","akey":"avalue"} `, `I output.go:] "odd parameters" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)" `: `{"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"} {"caller":"test/output.go:","msg":"odd parameters","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue"} `, `I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" anotherKeyWithoutValue="(MISSING)" I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" `: `{"caller":"test/output.go:","msg":"odd WithValues","v":0,"keyWithoutValue":"(MISSING)"} {"caller":"test/output.go:","msg":"odd WithValues","v":0,"keyWithoutValue":"(MISSING)","anotherKeyWithoutValue":"(MISSING)"} {"caller":"test/output.go:","msg":"odd WithValues","v":0,"keyWithoutValue":"(MISSING)"} `, `I output.go:] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)" `: `{"caller":"test/output.go:","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"} {"caller":"test/output.go:","msg":"both odd","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue"} `, `I output.go:] "test" akey9="avalue9" akey8="avalue8" akey1="avalue1" akey5="avalue5" akey4="avalue4" `: `{"caller":"test/output.go:","msg":"test","v":0,"akey9":"avalue9","akey8":"avalue8","akey1":"avalue1","akey5":"avalue5","akey4":"avalue4"} `, `I output.go:] "test" akey="avalue2" `: `{"caller":"test/output.go:","msg":"test","v":0,"akey":"avalue2"} `, `I output.go:] "test" X="y" duration="1m0s" A="b" `: `{"caller":"test/output.go:","msg":"test","v":0,"X":"y","duration":"1m0s","A":"b"} `, `I output.go:] "test" firstKey=1 I output.go:] "test" firstKey=1 secondKey=2 I output.go:] "test" firstKey=1 I output.go:] "test" firstKey=1 secondKey=3 `: `{"caller":"test/output.go:","msg":"test","v":0,"firstKey":1} {"caller":"test/output.go:","msg":"test","v":0,"firstKey":1,"secondKey":2} {"caller":"test/output.go:","msg":"test","v":0,"firstKey":1} {"caller":"test/output.go:","msg":"test","v":0,"firstKey":1,"secondKey":3} `, `I output.go:] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)" `: `{"caller":"test/output.go:","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1} {"caller":"test/output.go:","msg":"integer keys","v":0} `, `I output.go:] "struct keys" {name}="value" test="other value" key="val" `: `{"caller":"test/output.go:","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}} {"caller":"test/output.go:","msg":"struct keys","v":0} `, `I output.go:] "map keys" map[test:%!s(bool=true)]="test" `: `{"caller":"test/output.go:","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}} {"caller":"test/output.go:","msg":"map keys","v":0} `, // zapr does not support vmodule checks and thus always // discards these messages. `I output.go:] "v=11: you see me because of -vmodule output=11" `: ``, } { mapping[key] = value } return mapping }