Skip to content

Commit

Permalink
Merge pull request #163 from thockin/example-v-enabled
Browse files Browse the repository at this point in the history
Add examples for Logger methods
  • Loading branch information
pohly authored Nov 21, 2022
2 parents d4762a8 + b818fb2 commit 00ed9d0
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func NewStdoutLogger() logr.Logger {
return funcr.New(func(prefix, args string) {
if prefix != "" {
_ = fmt.Sprintf("%s: %s\n", prefix, args)
fmt.Printf("%s: %s\n", prefix, args)
} else {
fmt.Println(args)
}
Expand All @@ -44,3 +44,57 @@ func Example() {
// "level"=0 "msg"="V(0) info log" "stringVal"="value" "intVal"=12345
// "msg"="error log" "error"="an error" "stringVal"="value" "intVal"=12345
}

func ExampleLogger_Info() {
l := NewStdoutLogger()
l.Info("this is a V(0)-equivalent info log", "stringVal", "value", "intVal", 12345)
// Output:
// "level"=0 "msg"="this is a V(0)-equivalent info log" "stringVal"="value" "intVal"=12345
}

func ExampleLogger_Error() {
l := NewStdoutLogger()
l.Error(fmt.Errorf("the error"), "this is an error log", "stringVal", "value", "intVal", 12345)
l.Error(nil, "this is an error log with nil error", "stringVal", "value", "intVal", 12345)
// Output:
// "msg"="this is an error log" "error"="the error" "stringVal"="value" "intVal"=12345
// "msg"="this is an error log with nil error" "error"=null "stringVal"="value" "intVal"=12345
}

func ExampleLogger_WithName() {
l := NewStdoutLogger()
l = l.WithName("name1")
l.Info("this is an info log", "stringVal", "value", "intVal", 12345)
l = l.WithName("name2")
l.Info("this is an info log", "stringVal", "value", "intVal", 12345)
// Output:
// name1: "level"=0 "msg"="this is an info log" "stringVal"="value" "intVal"=12345
// name1/name2: "level"=0 "msg"="this is an info log" "stringVal"="value" "intVal"=12345
}

func ExampleLogger_WithValues() {
l := NewStdoutLogger()
l = l.WithValues("stringVal", "value", "intVal", 12345)
l = l.WithValues("boolVal", true)
l.Info("this is an info log", "floatVal", 3.1415)
// Output:
// "level"=0 "msg"="this is an info log" "stringVal"="value" "intVal"=12345 "boolVal"=true "floatVal"=3.1415
}

func ExampleLogger_V() {
l := NewStdoutLogger()
l.V(0).Info("V(0) info log")
l.V(1).Info("V(1) info log")
l.V(2).Info("V(2) info log")
// Output:
// "level"=0 "msg"="V(0) info log"
}

func ExampleLogger_Enabled() {
l := NewStdoutLogger()
if loggerV := l.V(5); loggerV.Enabled() {
// Do something expensive.
loggerV.Info("this is an expensive log message")
}
// Output:
}

0 comments on commit 00ed9d0

Please sign in to comment.