Using glog together with cobra in golang

2017-12-01

Cobra · Glog · Golang

2 minutes

If you have been programming with golang, you’ve probably heard of cobra. I use it extensively at work and also in my personal projects.

Recently though, I’ve been using glog more and more. And I quite like it. The thing is, it has a couple of flag definitions in its init() function using golang’s builtin flag library. And I wanted to include those flags into cobra’s flag definitions. This is how I did it.

Generated help information will now look something like this.

# run the help command
$ ./cobraglog -h
Use glog with cobra.

Usage:
   [flags]

Flags:
      --alsologtostderr                  log to standard error as well as files
      --echo string                      echo string (default "hello")
      --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
      --logtostderr                      log to standard error instead of files
      --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 this command

Note that our cobra-defined flag --echo is also there. The rest are defined by glog internally. Finally, run the application.

# run the binary, providing the logtostderr flag defined by glog
$ ./cobraglog --logtostderr
I1129 13:49:34.166660    2138 main.go:28] echo (info): hello
W1129 13:49:34.166718    2138 main.go:29] echo (warn): hello
E1129 13:49:34.166722    2138 main.go:30] echo (error): hello

Here’s another example using subcommands.