Debian Codegraph @

funcr/example_test.go

raw
1 /*
2 Copyright 2021 The logr 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 funcr_test
18
19 import (
20 "fmt"
21
22 "github.com/go-logr/logr/funcr"
23 )
24
25 func ExampleNew() {
26 log := funcr.New(func(prefix, args string) {
27 fmt.Println(prefix, args)
28 }, funcr.Options{})
29
30 log = log.WithName("MyLogger")
31 log = log.WithValues("savedKey", "savedValue")
32 log.Info("the message", "key", "value")
33 // Output: MyLogger "level"=0 "msg"="the message" "savedKey"="savedValue" "key"="value"
34 }
35
36 func ExampleNewJSON() {
37 log := funcr.NewJSON(func(obj string) {
38 fmt.Println(obj)
39 }, funcr.Options{})
40
41 log = log.WithName("MyLogger")
42 log = log.WithValues("savedKey", "savedValue")
43 log.Info("the message", "key", "value")
44 // Output: {"logger":"MyLogger","level":0,"msg":"the message","savedKey":"savedValue","key":"value"}
45 }
46
47 func ExampleUnderlier() {
48 log := funcr.New(func(prefix, args string) {
49 fmt.Println(prefix, args)
50 }, funcr.Options{})
51
52 if underlier, ok := log.GetSink().(funcr.Underlier); ok {
53 fn := underlier.GetUnderlying()
54 fn("hello", "world")
55 }
56 // Output: hello world
57 }
58
59 func ExampleOptions() {
60 log := funcr.NewJSON(
61 func(obj string) { fmt.Println(obj) },
62 funcr.Options{
63 LogCaller: funcr.All,
64 Verbosity: 1, // V(2) and higher is ignored.
65 })
66 log.V(0).Info("V(0) message", "key", "value")
67 log.V(1).Info("V(1) message", "key", "value")
68 log.V(2).Info("V(2) message", "key", "value")
69 // Output:
70 // {"logger":"","caller":{"file":"example_test.go","line":66},"level":0,"msg":"V(0) message","key":"value"}
71 // {"logger":"","caller":{"file":"example_test.go","line":67},"level":1,"msg":"V(1) message","key":"value"}
72 }
73
74 func ExampleOptions_renderHooks() {
75 // prefix all builtin keys with "log:"
76 prefixSpecialKeys := func(kvList []any) []any {
77 for i := 0; i < len(kvList); i += 2 {
78 k, _ := kvList[i].(string)
79 kvList[i] = "log:" + k
80 }
81 return kvList
82 }
83
84 // present saved values as a single JSON object
85 valuesAsObject := func(kvList []any) []any {
86 return []any{"labels", funcr.PseudoStruct(kvList)}
87 }
88
89 log := funcr.NewJSON(
90 func(obj string) { fmt.Println(obj) },
91 funcr.Options{
92 RenderBuiltinsHook: prefixSpecialKeys,
93 RenderValuesHook: valuesAsObject,
94 })
95 log = log.WithName("MyLogger")
96 log = log.WithValues("savedKey1", "savedVal1")
97 log = log.WithValues("savedKey2", "savedVal2")
98 log.Info("the message", "key", "value")
99 // Output: {"log:logger":"MyLogger","log:level":0,"log:msg":"the message","labels":{"savedKey1":"savedVal1","savedKey2":"savedVal2"},"key":"value"}
100 }
101
102 func ExamplePseudoStruct() {
103 log := funcr.NewJSON(
104 func(obj string) { fmt.Println(obj) },
105 funcr.Options{})
106 kv := []any{
107 "field1", 12345,
108 "field2", true,
109 }
110 log.Info("the message", "key", funcr.PseudoStruct(kv))
111 // Output: {"logger":"","level":0,"msg":"the message","key":{"field1":12345,"field2":true}}
112 }
113
114 func ExampleOptions_maxLogDepth() {
115 type List struct {
116 Next *List
117 }
118 l := List{}
119 l.Next = &l // recursive
120
121 log := funcr.NewJSON(
122 func(obj string) { fmt.Println(obj) },
123 funcr.Options{MaxLogDepth: 4})
124 log.Info("recursive", "list", l)
125 // Output: {"logger":"","level":0,"msg":"recursive","list":{"Next":{"Next":{"Next":{"Next":{"Next":"<max-log-depth-exceeded>"}}}}}}
126 }