Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of how to use golang/glog.
/*
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|WARN|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()
}
@arturoescaip

This comment has been minimized.

Copy link

commented Oct 4, 2016

You need to add a call to glog.Flush() here to guarantee that all output makes it to the log file.

@heatxsink

This comment has been minimized.

Copy link
Owner Author

commented Jun 6, 2017

Thanks!

@davidsnt

This comment has been minimized.

Copy link

commented Oct 11, 2017

glog.Flush() hint is a lifesaver.

@huanzheWu

This comment has been minimized.

Copy link

commented Oct 13, 2017

it's useful,thanks!

@linxiaobai

This comment has been minimized.

Copy link

commented Oct 16, 2017

-stderrthreshold=[INFO|WARN|FATAL]

are you sure there is WARN,rather than WARNING?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.