...
1
16
17
18 package example
19
20 import (
21 "context"
22 "errors"
23 "fmt"
24 "os"
25 "time"
26
27 "k8s.io/klog/v2"
28 )
29
30 func init() {
31
32 klog.TODO().Info("Oops, I shouldn't be logging yet!")
33 }
34
35 func Run(ctx context.Context) {
36 fmt.Println("This is normal output via stdout.")
37 fmt.Fprintln(os.Stderr, "This is other output via stderr.")
38 klog.Infof("Log using Infof, key: %s", "value")
39 klog.InfoS("Log using InfoS", "key", "value")
40 err := errors.New("fail")
41 klog.Errorf("Log using Errorf, err: %v", err)
42 klog.ErrorS(err, "Log using ErrorS")
43 klog.V(1).Info("Log less important message")
44
45
46
47 klog.TODO().Info("Now the default logger is set, but using the one from the context is still better.")
48
49 logger := klog.FromContext(ctx)
50 logger.V(5).Info("Log less important message at V=5 through context")
51
52
53
54 klog.LoggerWithValues(klog.LoggerWithName(logger, "myname"), "duration", time.Hour).Info("runtime", "duration", time.Minute)
55 logger.Info("another runtime", "duration", time.Hour, "duration", time.Minute)
56 }
57
View as plain text