Skip to content

Commit a47ebb9

Browse files
committedMay 15, 2020
Add example for co-existence of klog v1 and v2
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
1 parent a5796cb commit a47ebb9

File tree

2 files changed

+81
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)
Please sign in to comment.