gologgingerror-loggingglog

Set logging level in glog package


How can I set logging level for golang glog package to ERROR.

example.go:

package main

import (
    "github.com/golang/glog"
    "flag"
)

func main() {
    flag.Parse()
    glog.Info("Info level")
    glog.Error("Error level")
    glog.Flush()
}
$ go run example.go -logtostderr=true -stderrthreshold=ERROR
I1214 15:46:00.743002   13429 example.go:10] Info level
E1214 15:46:00.743211   13429 example.go:11] Error level

In the above example I have set the stderrthreshold flag to ERROR but am still getting INFO level logs. I want only ERROR level logs.


Solution

  • By default, glog will write

    By using the -logtostderr=true flag this behaviour is changed:

    Logs are written to standard error instead of to files.

    The stderrthreshold presumably has no effect when -logtostderr=true is used. And in your invocation it didn't have an effect anyway, because the default threshold is already ERROR.

    To summarize, by just running go run example.go without any command line arguments, only log messages with severity ERROR or above are written to stderr, as you desired.