| 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 logr_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
|
| 22 |
"github.com/go-logr/logr" |
| 23 |
"github.com/go-logr/logr/funcr" |
| 24 |
) |
| 25 |
|
| 26 |
// NewStdoutLogger returns a logr.Logger that prints to stdout. |
| 27 |
func NewStdoutLogger() logr.Logger { |
| 28 |
return funcr.New(func(prefix, args string) { |
| 29 |
if prefix != "" { |
| 30 |
fmt.Printf("%s: %s\n", prefix, args) |
| 31 |
} else { |
| 32 |
fmt.Println(args) |
| 33 |
} |
| 34 |
}, funcr.Options{}) |
| 35 |
} |
| 36 |
|
| 37 |
func Example() { |
| 38 |
l := NewStdoutLogger() |
| 39 |
l.Info("default info log", "stringVal", "value", "intVal", 12345) |
| 40 |
l.V(0).Info("V(0) info log", "stringVal", "value", "intVal", 12345) |
| 41 |
l.Error(fmt.Errorf("an error"), "error log", "stringVal", "value", "intVal", 12345) |
| 42 |
// Output: |
| 43 |
// "level"=0 "msg"="default info log" "stringVal"="value" "intVal"=12345 |
| 44 |
// "level"=0 "msg"="V(0) info log" "stringVal"="value" "intVal"=12345 |
| 45 |
// "msg"="error log" "error"="an error" "stringVal"="value" "intVal"=12345 |
| 46 |
} |
| 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 |
} |