...
1
16
17 package main
18
19 import (
20 stdlog "log"
21 "os"
22
23 "github.com/go-logr/logr"
24 "github.com/go-logr/stdr"
25 )
26
27 type e struct {
28 str string
29 }
30
31 func (e e) Error() string {
32 return e.str
33 }
34
35 func helper(log logr.Logger, msg string) {
36 helper2(log, msg)
37 }
38
39 func helper2(log logr.Logger, msg string) {
40 log.WithCallDepth(2).Info(msg)
41 }
42
43 func main() {
44 stdr.SetVerbosity(1)
45 log := stdr.NewWithOptions(stdlog.New(os.Stderr, "", stdlog.LstdFlags), stdr.Options{LogCaller: stdr.All})
46 log = log.WithName("MyName")
47 example(log.WithValues("module", "example"))
48 }
49
50
51 func example(log logr.Logger) {
52 log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
53 log.V(1).Info("you should see this")
54 log.V(1).V(1).Info("you should NOT see this")
55 log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
56 log.Error(e{"an error occurred"}, "goodbye", "code", -1)
57 helper(log, "thru a helper")
58 }
59
View as plain text