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