Skip to content

Instantly share code, notes, and snippets.

@heatxsink
Last active March 25, 2023 06:02
Show Gist options
  • Save heatxsink/7221ebe499b0767d4784 to your computer and use it in GitHub Desktop.
Save heatxsink/7221ebe499b0767d4784 to your computer and use it in GitHub Desktop.
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|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()
}
@arturoescaip
Copy link

arturoescaip 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
Copy link
Author

Thanks!

@davidsnt
Copy link

glog.Flush() hint is a lifesaver.

@huanzheWu
Copy link

it's useful,thanks!

@linxiaobai
Copy link

-stderrthreshold=[INFO|WARN|FATAL]

are you sure there is WARN,rather than WARNING?

@heatxsink
Copy link
Author

heatxsink commented Jan 28, 2020

-stderrthreshold=[INFO|WARN|FATAL]

are you sure there is WARN,rather than WARNING?

Thank you. It's definitely WARNING.

@heatxsink
Copy link
Author

it's useful,thanks!

You are very welcome!

@harshathulasi
Copy link

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?

@heatxsink
Copy link
Author

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()!

@CharmCcc
Copy link

May I ask you a question? When the log file is too large, could it be rotated automatically?

@heatxsink
Copy link
Author

May I ask you a question? When the log file is too large, could it be rotated automatically?

Yes rotated automatically.

@Masterxilo
Copy link

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?

@heatxsink
Copy link
Author

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