Skip to content

Commit b818fb2

Browse files
committed
Add examples for Logger methods
1 parent af0016d commit b818fb2

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

example_test.go

+55-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
func NewStdoutLogger() logr.Logger {
2828
return funcr.New(func(prefix, args string) {
2929
if prefix != "" {
30-
_ = fmt.Sprintf("%s: %s\n", prefix, args)
30+
fmt.Printf("%s: %s\n", prefix, args)
3131
} else {
3232
fmt.Println(args)
3333
}
@@ -44,3 +44,57 @@ func Example() {
4444
// "level"=0 "msg"="V(0) info log" "stringVal"="value" "intVal"=12345
4545
// "msg"="error log" "error"="an error" "stringVal"="value" "intVal"=12345
4646
}
47+
48+
func ExampleLogger_Info() {
49+
l := NewStdoutLogger()
50+
l.Info("this is a V(0)-equivalent info log", "stringVal", "value", "intVal", 12345)
51+
// Output:
52+
// "level"=0 "msg"="this is a V(0)-equivalent info log" "stringVal"="value" "intVal"=12345
53+
}
54+
55+
func ExampleLogger_Error() {
56+
l := NewStdoutLogger()
57+
l.Error(fmt.Errorf("the error"), "this is an error log", "stringVal", "value", "intVal", 12345)
58+
l.Error(nil, "this is an error log with nil error", "stringVal", "value", "intVal", 12345)
59+
// Output:
60+
// "msg"="this is an error log" "error"="the error" "stringVal"="value" "intVal"=12345
61+
// "msg"="this is an error log with nil error" "error"=null "stringVal"="value" "intVal"=12345
62+
}
63+
64+
func ExampleLogger_WithName() {
65+
l := NewStdoutLogger()
66+
l = l.WithName("name1")
67+
l.Info("this is an info log", "stringVal", "value", "intVal", 12345)
68+
l = l.WithName("name2")
69+
l.Info("this is an info log", "stringVal", "value", "intVal", 12345)
70+
// Output:
71+
// name1: "level"=0 "msg"="this is an info log" "stringVal"="value" "intVal"=12345
72+
// name1/name2: "level"=0 "msg"="this is an info log" "stringVal"="value" "intVal"=12345
73+
}
74+
75+
func ExampleLogger_WithValues() {
76+
l := NewStdoutLogger()
77+
l = l.WithValues("stringVal", "value", "intVal", 12345)
78+
l = l.WithValues("boolVal", true)
79+
l.Info("this is an info log", "floatVal", 3.1415)
80+
// Output:
81+
// "level"=0 "msg"="this is an info log" "stringVal"="value" "intVal"=12345 "boolVal"=true "floatVal"=3.1415
82+
}
83+
84+
func ExampleLogger_V() {
85+
l := NewStdoutLogger()
86+
l.V(0).Info("V(0) info log")
87+
l.V(1).Info("V(1) info log")
88+
l.V(2).Info("V(2) info log")
89+
// Output:
90+
// "level"=0 "msg"="V(0) info log"
91+
}
92+
93+
func ExampleLogger_Enabled() {
94+
l := NewStdoutLogger()
95+
if loggerV := l.V(5); loggerV.Enabled() {
96+
// Do something expensive.
97+
loggerV.Info("this is an expensive log message")
98+
}
99+
// Output:
100+
}

0 commit comments

Comments
 (0)