...

Source file src/k8s.io/klog/v2/test/zapr.go

Documentation: k8s.io/klog/v2/test

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  // ZaprOutputMappingDirect provides a mapping from klog output to the
    20  // corresponding zapr output when zapr is called directly.
    21  func ZaprOutputMappingDirect() map[string]string {
    22  	return map[string]string{
    23  		`I output.go:<LINE>] "test" akey="<&>"
    24  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"akey":"<&>"}
    25  `,
    26  
    27  		`E output.go:<LINE>] "test" err="whoops"
    28  `: `{"caller":"test/output.go:<LINE>","msg":"test","err":"whoops"}
    29  `,
    30  
    31  		`I output.go:<LINE>] "helper" akey="avalue"
    32  `: `{"caller":"test/output.go:<LINE>","msg":"helper","v":0,"akey":"avalue"}
    33  `,
    34  
    35  		`I output.go:<LINE>] "test" akey="avalue" akey="avalue2"
    36  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"akey":"avalue","akey":"avalue2"}
    37  `,
    38  
    39  		`I output.go:<LINE>] "test" logger="hello.world" akey="avalue"
    40  `: `{"logger":"hello.world","caller":"test/output.go:<LINE>","msg":"test","v":0,"akey":"avalue"}
    41  `,
    42  
    43  		`I output.go:<LINE>] "test" X="y" duration="1m0s" A="b"
    44  `: `{"caller":"test/output.go:<LINE>","msg":"test","duration":"1h0m0s","X":"y","v":0,"duration":"1m0s","A":"b"}
    45  `,
    46  
    47  		`I output.go:<LINE>] "test" akey9="avalue9" akey8="avalue8" akey1="avalue1" akey5="avalue5" akey4="avalue4"
    48  `: `{"caller":"test/output.go:<LINE>","msg":"test","akey9":"avalue9","akey8":"avalue8","akey1":"avalue1","v":0,"akey5":"avalue5","akey4":"avalue4"}
    49  `,
    50  
    51  		`I output.go:<LINE>] "test"
    52  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0}
    53  `,
    54  
    55  		`I output.go:<LINE>] "\"quoted\"" key="\"quoted value\""
    56  `: `{"caller":"test/output.go:<LINE>","msg":"\"quoted\"","v":0,"key":"\"quoted value\""}
    57  `,
    58  
    59  		`I output.go:<LINE>] "test" err="whoops"
    60  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"err":"whoops"}
    61  `,
    62  
    63  		`I output.go:<LINE>] "test" pod="kube-system/pod-1"
    64  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pod":{"name":"pod-1","namespace":"kube-system"}}
    65  `,
    66  
    67  		`I output.go:<LINE>] "KObjs" pods=[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]
    68  `: `{"caller":"test/output.go:<LINE>","msg":"KObjs","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]}
    69  `,
    70  
    71  		`I output.go:<LINE>] "KObjSlice" pods=["kube-system/pod-1","kube-system/pod-2"]
    72  `: `{"caller":"test/output.go:<LINE>","msg":"KObjSlice","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]}
    73  `,
    74  
    75  		`I output.go:<LINE>] "test" pods=null
    76  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pods":null}
    77  `,
    78  
    79  		`I output.go:<LINE>] "test" pods="<KObjSlice needs a slice, got type int>"
    80  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pods":"<KObjSlice needs a slice, got type int>"}
    81  `,
    82  
    83  		`I output.go:<LINE>] "test" ints=["<KObjSlice needs a slice of values implementing KMetadata, got type int>"]
    84  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"ints":"<KObjSlice needs a slice of values implementing KMetadata, got type int>"}
    85  `,
    86  
    87  		`I output.go:<LINE>] "test" pods=["kube-system/pod-1",null]
    88  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},null]}
    89  `,
    90  
    91  		`I output.go:<LINE>] "test" akey="avalue"
    92  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"akey":"avalue"}
    93  `,
    94  
    95  		`I output.go:<LINE>] "test" logger="me" akey="avalue"
    96  `: `{"logger":"me","caller":"test/output.go:<LINE>","msg":"test","v":0,"akey":"avalue"}
    97  `,
    98  
    99  		`I output.go:<LINE>] "test" akey="avalue2"
   100  `: `{"caller":"test/output.go:<LINE>","msg":"test","akey":"avalue","v":0,"akey":"avalue2"}
   101  `,
   102  
   103  		`I output.go:<LINE>] "you see me"
   104  `: `{"caller":"test/output.go:<LINE>","msg":"you see me","v":9}
   105  `,
   106  
   107  		`I output.go:<LINE>] "test" firstKey=1
   108  I output.go:<LINE>] "test" firstKey=1 secondKey=2
   109  I output.go:<LINE>] "test" firstKey=1
   110  I output.go:<LINE>] "test" firstKey=1 secondKey=3
   111  `: `{"caller":"test/output.go:<LINE>","msg":"test","firstKey":1,"v":0}
   112  {"caller":"test/output.go:<LINE>","msg":"test","firstKey":1,"secondKey":2,"v":0}
   113  {"caller":"test/output.go:<LINE>","msg":"test","firstKey":1,"v":0}
   114  {"caller":"test/output.go:<LINE>","msg":"test","firstKey":1,"secondKey":3,"v":0}
   115  `,
   116  
   117  		`I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
   118  I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)" anotherKeyWithoutValue="(MISSING)"
   119  I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
   120  `: `{"caller":"test/output.go:<WITH-VALUES>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"keyWithoutValue"}
   121  {"caller":"test/output.go:<WITH-VALUES-2>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"anotherKeyWithoutValue"}
   122  {"caller":"test/output.go:<LINE>","msg":"odd WithValues","v":0}
   123  {"caller":"test/output.go:<LINE>","msg":"odd WithValues","v":0}
   124  {"caller":"test/output.go:<LINE>","msg":"odd WithValues","v":0}
   125  `,
   126  
   127  		`I output.go:<LINE>] "odd arguments" akey="avalue" akey2="(MISSING)"
   128  `: `{"caller":"test/output.go:<LINE>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"}
   129  {"caller":"test/output.go:<LINE>","msg":"odd arguments","v":0,"akey":"avalue"}
   130  `,
   131  
   132  		`I output.go:<LINE>] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
   133  `: `{"caller":"test/output.go:<WITH-VALUES>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"basekey2"}
   134  {"caller":"test/output.go:<LINE>","msg":"odd number of arguments passed as key-value pairs for logging","basekey1":"basevar1","ignored key":"akey2"}
   135  {"caller":"test/output.go:<LINE>","msg":"both odd","basekey1":"basevar1","v":0,"akey":"avalue"}
   136  `,
   137  
   138  		`I output.go:<LINE>] "marshaler nil" obj="<panic: value method k8s.io/klog/v2.ObjectRef.WriteText called using nil *ObjectRef pointer>"
   139  `: `{"caller":"test/output.go:<LINE>","msg":"marshaler nil","v":0,"objError":"PANIC=value method k8s.io/klog/v2.ObjectRef.MarshalLog called using nil *ObjectRef pointer"}
   140  `,
   141  
   142  		// zap replaces a panic for a nil object with <nil>.
   143  		`E output.go:<LINE>] "error nil" err="<panic: runtime error: invalid memory address or nil pointer dereference>"
   144  `: `{"caller":"test/output.go:<LINE>","msg":"error nil","err":"<nil>"}
   145  `,
   146  
   147  		`I output.go:<LINE>] "stringer nil" stringer="<panic: runtime error: invalid memory address or nil pointer dereference>"
   148  `: `{"caller":"test/output.go:<LINE>","msg":"stringer nil","v":0,"stringer":"<nil>"}
   149  `,
   150  
   151  		`I output.go:<LINE>] "stringer panic" stringer="<panic: fake String panic>"
   152  `: `{"caller":"test/output.go:<LINE>","msg":"stringer panic","v":0,"stringerError":"PANIC=fake String panic"}
   153  `,
   154  
   155  		`E output.go:<LINE>] "error panic" err="<panic: fake Error panic>"
   156  `: `{"caller":"test/output.go:<LINE>","msg":"error panic","errError":"PANIC=fake Error panic"}
   157  `,
   158  
   159  		`I output.go:<LINE>] "marshaler panic" obj="<panic: fake MarshalLog panic>"
   160  `: `{"caller":"test/output.go:<LINE>","msg":"marshaler panic","v":0,"objError":"PANIC=fake MarshalLog panic"}
   161  `,
   162  
   163  		`I output.go:<LINE>] "marshaler recursion" obj={}
   164  `: `{"caller":"test/output.go:<LINE>","msg":"marshaler recursion","v":0,"obj":{}}
   165  `,
   166  
   167  		// klog.Info
   168  		`I output.go:<LINE>] "helloworld\n"
   169  `: `{"caller":"test/output.go:<LINE>","msg":"helloworld","v":0}
   170  `,
   171  
   172  		// klog.Infoln
   173  		`I output.go:<LINE>] "hello world\n"
   174  `: `{"caller":"test/output.go:<LINE>","msg":"hello world","v":0}
   175  `,
   176  
   177  		// klog.Error
   178  		`E output.go:<LINE>] "helloworld\n"
   179  `: `{"caller":"test/output.go:<LINE>","msg":"helloworld"}
   180  `,
   181  
   182  		// klog.Errorln
   183  		`E output.go:<LINE>] "hello world\n"
   184  `: `{"caller":"test/output.go:<LINE>","msg":"hello world"}
   185  `,
   186  
   187  		// klog.ErrorS
   188  		`E output.go:<LINE>] "world" err="hello"
   189  `: `{"caller":"test/output.go:<LINE>","msg":"world","err":"hello"}
   190  `,
   191  
   192  		// klog.InfoS
   193  		`I output.go:<LINE>] "hello" what="world"
   194  `: `{"caller":"test/output.go:<LINE>","msg":"hello","v":0,"what":"world"}
   195  `,
   196  
   197  		// klog.V(1).Info
   198  		`I output.go:<LINE>] "hellooneworld\n"
   199  `: `{"caller":"test/output.go:<LINE>","msg":"hellooneworld","v":1}
   200  `,
   201  
   202  		// klog.V(1).Infoln
   203  		`I output.go:<LINE>] "hello one world\n"
   204  `: `{"caller":"test/output.go:<LINE>","msg":"hello one world","v":1}
   205  `,
   206  
   207  		// klog.V(1).ErrorS
   208  		`E output.go:<LINE>] "one world" err="hello"
   209  `: `{"caller":"test/output.go:<LINE>","msg":"one world","err":"hello"}
   210  `,
   211  
   212  		// klog.V(1).InfoS
   213  		`I output.go:<LINE>] "hello" what="one world"
   214  `: `{"caller":"test/output.go:<LINE>","msg":"hello","v":1,"what":"one world"}
   215  `,
   216  
   217  		`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
   218  `: `{"caller":"test/output.go:<WITH-VALUES>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
   219  {"caller":"test/output.go:<LINE>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"}
   220  {"caller":"test/output.go:<LINE>","msg":"integer keys","v":0,"akey":"avalue"}
   221  `,
   222  
   223  		`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
   224  `: `{"caller":"test/output.go:<WITH-VALUES>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
   225  {"caller":"test/output.go:<LINE>","msg":"struct keys","v":0,"key":"val"}
   226  `,
   227  		`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
   228  `: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
   229  {"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
   230  `,
   231  
   232  		`I output.go:<LINE>] "Format" config=<
   233  	{
   234  	  "Kind": "config",
   235  	  "RealField": 42
   236  	}
   237   >
   238  `: `{"caller":"test/output.go:<LINE>","msg":"Format","v":0,"config":{"Kind":"config","RealField":42}}
   239  `,
   240  		`I output.go:<LINE>] "maps" s={"hello":"world"} i={"1":2,"3":4}
   241  `: `{"caller":"test/output.go:<LINE>","msg":"maps","v":0,"s":{"hello":"world"},"i":{"1":2,"3":4}}
   242  `,
   243  
   244  		`I output.go:<LINE>] "slices" s=["hello","world"] i=[1,2,3]
   245  `: `{"caller":"test/output.go:<LINE>","msg":"slices","v":0,"s":["hello","world"],"i":[1,2,3]}
   246  `,
   247  		`I output.go:<LINE>] "structs" s={"Name":"worker","Kind":"pod"}
   248  `: `{"caller":"test/output.go:<LINE>","msg":"structs","v":0,"s":{"Name":"worker","Kind":"pod"}}
   249  `,
   250  		`I output.go:<LINE>] "klog.Format" s=<
   251  	{
   252  	  "Name": "worker",
   253  	  "Kind": "pod"
   254  	}
   255   >
   256  `: `{"caller":"test/output.go:<LINE>","msg":"klog.Format","v":0,"s":{"Name":"worker","Kind":"pod"}}
   257  `,
   258  
   259  		`I output.go:<LINE>] "cycle" list="<internal error: json: unsupported value: encountered a cycle via *test.myList>"
   260  `: `{"caller":"test/output.go:<LINE>","msg":"cycle","v":0,"listError":"json: unsupported value: encountered a cycle via *test.myList"}
   261  `,
   262  	}
   263  }
   264  
   265  // ZaprOutputMappingIndirect provides a mapping from klog output to the
   266  // corresponding zapr output when zapr is called indirectly through
   267  // klog.
   268  //
   269  // This is different from ZaprOutputMappingDirect because:
   270  //   - WithName gets added to the message by Output.
   271  //   - zap uses . as separator instead of / between WithName values,
   272  //     here we get slashes because Output concatenates these values.
   273  //   - WithValues are added to the normal key/value parameters by
   274  //     Output, which puts them after "v".
   275  //   - Output does that without emitting the warning that we get
   276  //     from zapr.
   277  //   - zap drops keys with missing values, here we get "(MISSING)".
   278  //   - zap does not de-duplicate key/value pairs, here klog does that
   279  //     for it.
   280  func ZaprOutputMappingIndirect() map[string]string {
   281  	mapping := ZaprOutputMappingDirect()
   282  
   283  	for key, value := range map[string]string{
   284  		`I output.go:<LINE>] "test" logger="hello.world" akey="avalue"
   285  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"logger":"hello.world","akey":"avalue"}
   286  `,
   287  
   288  		`I output.go:<LINE>] "test" logger="me" akey="avalue"
   289  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"logger":"me","akey":"avalue"}
   290  `,
   291  
   292  		`I output.go:<LINE>] "odd parameters" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
   293  `: `{"caller":"test/output.go:<LINE>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"}
   294  {"caller":"test/output.go:<LINE>","msg":"odd parameters","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue"}
   295  `,
   296  
   297  		`I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
   298  I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)" anotherKeyWithoutValue="(MISSING)"
   299  I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
   300  `: `{"caller":"test/output.go:<LINE>","msg":"odd WithValues","v":0,"keyWithoutValue":"(MISSING)"}
   301  {"caller":"test/output.go:<LINE>","msg":"odd WithValues","v":0,"keyWithoutValue":"(MISSING)","anotherKeyWithoutValue":"(MISSING)"}
   302  {"caller":"test/output.go:<LINE>","msg":"odd WithValues","v":0,"keyWithoutValue":"(MISSING)"}
   303  `,
   304  
   305  		`I output.go:<LINE>] "both odd" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
   306  `: `{"caller":"test/output.go:<LINE>","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"akey2"}
   307  {"caller":"test/output.go:<LINE>","msg":"both odd","v":0,"basekey1":"basevar1","basekey2":"(MISSING)","akey":"avalue"}
   308  `,
   309  
   310  		`I output.go:<LINE>] "test" akey9="avalue9" akey8="avalue8" akey1="avalue1" akey5="avalue5" akey4="avalue4"
   311  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"akey9":"avalue9","akey8":"avalue8","akey1":"avalue1","akey5":"avalue5","akey4":"avalue4"}
   312  `,
   313  
   314  		`I output.go:<LINE>] "test" akey="avalue2"
   315  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"akey":"avalue2"}
   316  `,
   317  
   318  		`I output.go:<LINE>] "test" X="y" duration="1m0s" A="b"
   319  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"X":"y","duration":"1m0s","A":"b"}
   320  `,
   321  
   322  		`I output.go:<LINE>] "test" firstKey=1
   323  I output.go:<LINE>] "test" firstKey=1 secondKey=2
   324  I output.go:<LINE>] "test" firstKey=1
   325  I output.go:<LINE>] "test" firstKey=1 secondKey=3
   326  `: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1}
   327  {"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1,"secondKey":2}
   328  {"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1}
   329  {"caller":"test/output.go:<LINE>","msg":"test","v":0,"firstKey":1,"secondKey":3}
   330  `,
   331  		`I output.go:<LINE>] "integer keys" %!s(int=1)="value" %!s(int=2)="value2" akey="avalue" akey2="(MISSING)"
   332  `: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":1}
   333  {"caller":"test/output.go:<LINE>","msg":"integer keys","v":0}
   334  `,
   335  		`I output.go:<LINE>] "struct keys" {name}="value" test="other value" key="val"
   336  `: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{}}
   337  {"caller":"test/output.go:<LINE>","msg":"struct keys","v":0}
   338  `,
   339  		`I output.go:<LINE>] "map keys" map[test:%!s(bool=true)]="test"
   340  `: `{"caller":"test/output.go:<LINE>","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":{"test":true}}
   341  {"caller":"test/output.go:<LINE>","msg":"map keys","v":0}
   342  `,
   343  
   344  		// zapr does not support vmodule checks and thus always
   345  		// discards these messages.
   346  		`I output.go:<LINE>] "v=11: you see me because of -vmodule output=11"
   347  `: ``,
   348  	} {
   349  		mapping[key] = value
   350  	}
   351  	return mapping
   352  }
   353  

View as plain text