-
-
Save heatxsink/7221ebe499b0767d4784 to your computer and use it in GitHub Desktop.
| /* | |
| 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!
glog.Flush() hint is a lifesaver.
it's useful,thanks!
-stderrthreshold=[INFO|WARN|FATAL]
are you sure there is WARN,rather than WARNING?
-stderrthreshold=[INFO|WARN|FATAL]
are you sure there is WARN,rather than WARNING?
Thank you. It's definitely WARNING.
it's useful,thanks!
You are very welcome!
So what happens if i abort (cntrl-c) before the glog.flush() is issued? is it just not guaranteed but possible to be written to file?
So what happens if i abort (cntrl-c) before the glog.flush() is issued? is it just not guaranteed but possible to be written to file?
Your code that consumes glog should handle SIGINT (ctrl-c), and do one last glog.Flush()!
May I ask you a question? When the log file is too large, could it be rotated automatically?
May I ask you a question? When the log file is too large, could it be rotated automatically?
Yes rotated automatically.
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. :(
You need to add a call to glog.Flush() here to guarantee that all output makes it to the log file.