go - 在 glog 包中设置日志记录级别

标签 go logging error-logging glog

如何设置 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/

相关文章:

Mysql - 获取错误值

sails.js - 我正在使用 sailsjs,我想将 winston-sentry 与它集成。我在向 Sentry 添加元数据方面运气不佳。我的代码如下 :

go - 如何在 golang 中检查 NaN

如果使用字段中具有默认值的 Struct 实例进行过滤,Go-gorm 将返回所有记录

php - 将 PHP 错误发送到默认错误日志和用户定义的错误日志

c# - log4Net RollingFileAppender 没有 "Roll"

.net - 有意义地精简异常/Environment.StackTrace

bash - 如何使用 Go 在命令行上执行 diff?

go - Go 例程后关闭冗余 sql.Rows 对象的推荐方法

spring-boot - Spring Boot 应用程序意外关闭并记录问题