I have a project that needs to use cobra
and klog
to generate an executable file and Print the log and keep it.
First I tested it. Using the following klog
can output the log to terminal and file
at the same time.
package main import ( "flag" "k8s.io/klog" ) func init() { var fs flag.flagset klog.initflags(&fs) fs.set("logtostderr", "false") fs.set("log_file_max_size", "100") fs.set("log_file", "/home/test/workspace/klogfile/test.log") fs.set("alsologtostderr", "true") } func main() { defer klog.flush() klog.info("info") klog.warning("warning") klog.error("error") }
The contents of the file/home/test/workspace/klogfile/test.log
are as follows
log file created at: 2023/04/06 16:46:07 running on machine: s52 binary: built with gc go1.16.12 for linux/amd64 log line format: [iwef]mmdd hh:mm:ss.uuuuuu threadid file:line] msg i0406 16:46:07.751183 13512 main.go:20] info w0406 16:46:07.751594 13512 main.go:21] warning e0406 16:46:07.751629 13512 main.go:22] error
Then I tried adding to cobra
based on
k8s style
package main import ( "flag" "github.com/spf13/cobra" "k8s.io/klog" ) var ( str = "hello world" ) func newcommand() *cobra.command { cmd := &cobra.command{ use: "echo", short: "use klog with cobra", long: "use klog together with cobra.", run: func(cmd *cobra.command, args []string) { run() }, } klog.initflags(flag.commandline) cmd.flags().addgoflagset(flag.commandline) cmd.flags().sortflags = false cmd.flags().stringvar(&str, "str", str, "string to print") return cmd } func run() { defer klog.flush() klog.infof("running, str:%s", str) } func main() { if err := newcommand().execute(); err != nil { klog.fatalf("root cmd execute failed, err=%v", err) } }
I can print out the required log options
via --help
, but when I override them to achieve saving to file as in the example above, only the output Output to terminal instead of file< /strong>
# go run main2.go --logtostderr false --log_file_max_size "100" --alsologtostderr true --log_file "/home/test/workspace/klogfile/test2.log" I0406 16:52:57.479455 15217 cobra_klog.go:34] Running, str:hello world
May I ask where is missing the necessary code
? If there is any, please tell me how to modify it, thank you!
There is nothing wrong with your code. The only problem is that you must use the -flag=false
form to turn off the boolean flag (see Command Line Flag Syntax).
Try this command (replace --logtostderr false
with --logtostderr=false
):
go run main2.go --logtostderr=false --log_file_max_size "100" --alsologtostderr true --log_file "/home/test/workspace/klogfile/test2.log"
By the way, I think --log_file_max_size "100"
can be simplified to --log_file_max_size 100
.
renew:
Reduce klog exposed flags:
package main import ( "flag" "github.com/spf13/cobra" "k8s.io/klog" ) var ( str = "hello world" + logFile string ) func NewCommand() *cobra.Command { + var fs flag.FlagSet + klog.InitFlags(&fs) cmd := &cobra.Command{ Use: "echo", Short: "use klog with cobra", Long: "Use klog together with cobra.", Run: func(cmd *cobra.Command, args []string) { + fs.Set("logtostderr", "false") + fs.Set("log_file", logFile) Run() }, } - klog.InitFlags(flag.CommandLine) - cmd.Flags().AddGoFlagSet(flag.CommandLine) cmd.Flags().SortFlags = false cmd.Flags().StringVar(&str, "str", str, "string to print") + cmd.Flags().StringVar(&logFile, "log_file", "", "If non-empty, use this log file") return cmd } func Run() { defer klog.Flush() klog.Infof("Running, str:%s", str) } func main() { if err := NewCommand().Execute(); err != nil { klog.Fatalf("root cmd execute failed, err=%v", err) } }
The above is the detailed content of How to combine cobra and klog. For more information, please follow other related articles on the PHP Chinese website!