好吧,我浏览了文档,并且 glog 由标志控制,因此必须存在它们才能使其正常工作。在您的情况下,您必须在运行时设置它们。您将要设置logtostderr=true
日志到 stderr 并将严重性级别设置为可能的选项之一:
stderrthreshold=[WARNING|ERROR|INFO|FATAL]
例子:
package main
import (
"flag"
"fmt"
"os"
"github.com/golang/glog"
)
func usage() {
flag.PrintDefaults()
os.Exit(2)
}
func init() {
flag.Usage = usage
flag.Set("logtostderr", "true")
flag.Set("stderrthreshold", "WARNING")
flag.Set("v", "2")
// This is wa
flag.Parse()
}
func main() {
number_of_lines := 10
for i := 0; i < number_of_lines; i++ {
glog.V(2).Infof("LINE: %d", i)
message := fmt.Sprintf("TEST LINE: %d", i)
glog.Warning(message)
}
glog.Flush()
}
输出:
$ ./stackoverflow.exe
I0615 11:03:47.589969 11776 main.go:30] LINE: 0
W0615 11:03:47.590469 11776 main.go:32] TEST LINE: 0
I0615 11:03:47.590969 11776 main.go:30] LINE: 1
W0615 11:03:47.590969 11776 main.go:32] TEST LINE: 1
I0615 11:03:47.590969 11776 main.go:30] LINE: 2
W0615 11:03:47.590969 11776 main.go:32] TEST LINE: 2
I0615 11:03:47.590969 11776 main.go:30] LINE: 3
W0615 11:03:47.590969 11776 main.go:32] TEST LINE: 3
I0615 11:03:47.590969 11776 main.go:30] LINE: 4
W0615 11:03:47.590969 11776 main.go:32] TEST LINE: 4
I0615 11:03:47.591469 11776 main.go:30] LINE: 5
W0615 11:03:47.591469 11776 main.go:32] TEST LINE: 5
I0615 11:03:47.591469 11776 main.go:30] LINE: 6
W0615 11:03:47.591469 11776 main.go:32] TEST LINE: 6
I0615 11:03:47.591469 11776 main.go:30] LINE: 7
W0615 11:03:47.591469 11776 main.go:32] TEST LINE: 7
I0615 11:03:47.591469 11776 main.go:30] LINE: 8
W0615 11:03:47.591469 11776 main.go:32] TEST LINE: 8
I0615 11:03:47.591469 11776 main.go:30] LINE: 9
W0615 11:03:47.591469 11776 main.go:32] TEST LINE: 9