Using k8s.io/klog together with cobra in golang

2019-02-05

Cobra · Golang · Klog

2 minutes

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.

# 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