Debian Codegraph @

examples/coexist_klog_v1_and_v2/coexist_klog_v1_and_v2.go

raw symbols SCIP
1 package main
2
3 import (
4 "flag"
5
6 klogv1 "k8s.io/klog"
7 klogv2 "k8s.io/klog/v2"
8 )
9
10 // OutputCallDepth is the stack depth where we can find the origin of this call
11 const OutputCallDepth = 6
12
13 // DefaultPrefixLength is the length of the log prefix that we have to strip out
14 const DefaultPrefixLength = 53
15
16 // klogWriter is used in SetOutputBySeverity call below to redirect
17 // any calls to klogv1 to end up in klogv2
18 type klogWriter struct{}
19
20 func (kw klogWriter) Write(p []byte) (n int, err error) {
21 if len(p) < DefaultPrefixLength {
22 klogv2.InfoDepth(OutputCallDepth, string(p))
23 return len(p), nil
24 }
25 if p[0] == 'I' {
26 klogv2.InfoDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
27 } else if p[0] == 'W' {
28 klogv2.WarningDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
29 } else if p[0] == 'E' {
30 klogv2.ErrorDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
31 } else if p[0] == 'F' {
32 klogv2.FatalDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
33 } else {
34 klogv2.InfoDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
35 }
36 return len(p), nil
37 }
38
39 func main() {
40 // initialize klog/v2, can also bind to a local flagset if desired
41 klogv2.InitFlags(nil)
42
43 // In this example, we want to show you that all the lines logged
44 // end up in the myfile.log. You do NOT need them in your application
45 // as all these flags are set up from the command line typically
46 flag.Set("logtostderr", "false") // By default klog logs to stderr, switch that off
47 flag.Set("alsologtostderr", "false") // false is default, but this is informative
48 flag.Set("stderrthreshold", "FATAL") // stderrthreshold defaults to ERROR, we don't want anything in stderr
49 flag.Set("log_file", "myfile.log") // log to a file
50
51 // parse klog/v2 flags
52 flag.Parse()
53 // make sure we flush before exiting
54 defer klogv2.Flush()
55
56 // BEGIN : hack to redirect klogv1 calls to klog v2
57 // Tell klog NOT to log into STDERR. Otherwise, we risk
58 // certain kinds of API errors getting logged into a directory not
59 // available in a `FROM scratch` Docker container, causing us to abort
60 var klogv1Flags flag.FlagSet
61 klogv1.InitFlags(&klogv1Flags)
62 klogv1Flags.Set("logtostderr", "false") // By default klog v1 logs to stderr, switch that off
63 klogv1Flags.Set("stderrthreshold", "FATAL") // stderrthreshold defaults to ERROR, use this if you
64 // don't want anything in your stderr
65
66 klogv1.SetOutputBySeverity("INFO", klogWriter{}) // tell klog v1 to use the writer
67 // END : hack to redirect klogv1 calls to klog v2
68
69 // Now you can mix klogv1 and v2 in the same code base
70 klogv2.Info("hello from klog (v2)!")
71 klogv1.Info("hello from klog (v1)!")
72 klogv1.Warning("beware from klog (v1)!")
73 klogv1.Error("error from klog (v1)!")
74 klogv2.Info("nice to meet you (v2)")
75 }