This post is somehow related to a previous article about using glog together with cobra. This time, we will be using klog which is a Kubernetes fork of glog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
goflag "flag" | |
"github.com/spf13/cobra" | |
flag "github.com/spf13/pflag" | |
"k8s.io/klog" | |
) | |
var ( | |
str = "hello world" | |
rootCmd = &cobra.Command{ | |
Use: "echo", | |
Short: "use klog with cobra", | |
Long: "Use klog together with cobra.", | |
} | |
) | |
func init() { | |
rootCmd.Flags().SortFlags = false | |
rootCmd.AddCommand( | |
RunCmd(), | |
) | |
klog.InitFlags(nil) | |
goflag.Parse() | |
flag.CommandLine.AddGoFlagSet(goflag.CommandLine) | |
} | |
func RunCmd() *cobra.Command { | |
runcmd := &cobra.Command{ | |
Use: "run", | |
Short: "run command", | |
Long: "Run command.", | |
Run: func(cmd *cobra.Command, args []string) { | |
klog.Infof("echo=%v", str) | |
}, | |
} | |
runcmd.Flags().SortFlags = false | |
runcmd.Flags().StringVar(&str, "str", str, "string to print") | |
return runcmd | |
} | |
func main() { | |
if err := rootCmd.Execute(); err != nil { | |
klog.Fatalf("root cmd execute failed, err=%v", err) | |
} | |
} |
# run the -h command
$ ./cobraklog -h
Usage of ./cobraklog:
-alsologtostderr
log to standard error as well as files
-log_backtrace_at value
when logging hits line file:N, emit a stack trace
-log_dir string
If non-empty, write log files in this directory
-log_file string
If non-empty, use this log file
-logtostderr
log to standard error instead of files
-skip_headers
If true, avoid header prefixes in the log messages
-stderrthreshold value
logs at or above this threshold go to stderr (default 2)
-v value
log level for V logs
-vmodule value
comma-separated list of pattern=N settings for file-filtered logging
# run cobra's `help` command
$ ./cobraklog help
Use klog together with cobra.
Usage:
cobraklog [command]
Available Commands:
help Help about any command
run run command
Flags:
--alsologtostderr log to standard error as well as files
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory
--log_file string If non-empty, use this log file
--logtostderr log to standard error instead of files
--skip_headers If true, avoid header prefixes in the log messages
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
-v, --v Level log level for V logs
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
-h, --help help for cobraklog
Use "cobraklog [command] --help" for more information about a command.
# run `help` on our subcommand `run`
$ ./cobraklog help run
Run command.
Usage:
cobraklog run [flags]
Flags:
--str string string to print (default "hello world")
-h, --help help for run
Global Flags:
--alsologtostderr log to standard error as well as files
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory
--log_file string If non-empty, use this log file
--logtostderr log to standard error instead of files
--skip_headers If true, avoid header prefixes in the log messages
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
-v, --v Level log level for V logs
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
# run the `run` subcommand
$ ./cobraklog run --logtostderr
I0205 16:37:39.110849 13672 main.go:41] echo=hello world
$ ./cobraklog run --logtostderr --str "hello alien world"
I0205 16:39:37.212187 13685 main.go:41] echo=hello alien world