如何设置 golang 的日志记录级别 glog包错误。
示例.go:
package main
import (
"github.com/golang/glog"
"flag"
)
func main() {
flag.Parse()
glog.Info("Info level")
glog.Error("Error level")
glog.Flush()
}
$ go run example.go -logtostderr=true -stderrthreshold=ERROR
I1214 15:46:00.743002 13429 example.go:10] Info level
E1214 15:46:00.743211 13429 example.go:11] Error level
在上面的示例中,我已将 stderrthreshold
标志设置为 ERROR,但仍然收到 INFO 级别日志。我只想要错误级别的日志。
最佳答案
默认情况下,glog 会写入
- 将所有日志消息记录到日志文件(默认情况下,它们在
/tmp
中创建); - 将严重性为
stderrthreshold
或以上的消息记录到 stderr。
通过使用 -logtostderr=true
标志,此行为已更改:
Logs are written to standard error instead of to files.
- 没有日志消息写入日志文件;
- 所有日志消息都会写入 stderr。
使用 -logtostderr=true
时,stderrthreshold
可能不起作用。在您的调用中它无论如何都没有效果,因为默认阈值已经是ERROR
。
总而言之,通过仅运行 go run example.go
而不使用任何命令行参数,只有严重性为 ERROR
或以上的日志消息才会根据您的需要写入 stderr .
关于go - 在 glog 包中设置日志记录级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65287359/