Last active
March 25, 2023 06:02
-
-
Save heatxsink/7221ebe499b0767d4784 to your computer and use it in GitHub Desktop.
An example of how to use golang/glog.
This file contains 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
/* | |
glog-example | |
------------ | |
background | |
--- | |
You probably want to read the source code comments at the top of the glog.go file in | |
the golang/glog repository on github.com. Located here: https://github.com/golang/glog/blob/master/glog.go | |
setup | |
--- | |
$ go get github.com/golang/glog | |
$ mkdir log | |
run | |
--- | |
$ go run example.go -stderrthreshold=FATAL -log_dir=./log | |
or | |
$ go run example.go -stderrthreshold=FATAL -log_dir=./log -v=2 | |
or | |
$ go run example.go -logtostderr=true | |
or | |
$ go run example.go -logtostderr=true -v=2 | |
*/ | |
package main | |
import ( | |
"github.com/golang/glog" | |
"os" | |
"flag" | |
"fmt" | |
) | |
func usage() { | |
fmt.Fprintf(os.Stderr, "usage: example -stderrthreshold=[INFO|WARNING|FATAL] -log_dir=[string]\n", ) | |
flag.PrintDefaults() | |
os.Exit(2) | |
} | |
func init() { | |
flag.Usage = usage | |
// NOTE: This next line is key you have to call flag.Parse() for the command line | |
// options or "flags" that are defined in the glog module to be picked up. | |
flag.Parse() | |
} | |
func main() { | |
number_of_lines := 100000 | |
for i := 0; i < number_of_lines; i++ { | |
glog.V(2).Infof("LINE: %d", i) | |
message := fmt.Sprintf("TEST LINE: %d", i) | |
glog.Error(message) | |
} | |
glog.Flush() | |
} |
Thanks! Question: does golang/glog also support supplying the options via environment variables instead of command line flags? In https://github.com/google/glog#user-guide it says we can do
GLOG_logtostderr=1 ./your_application
is that possible in the go version?
I browsed the source located here: https://github.com/golang/glog
It appears there is no support for environment variables in this implementation. :(
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes rotated automatically.