duan1930 2018-06-15 15:45
浏览 1543
已采纳

我可以在Glog中设置全局日志记录级别吗?

In Java, which is the language in which I normally work, a common feature of logging libraries is the ability to set a "global" logging level - eg emit to the log file if the level is WARNING or above, otherwise not, regardless of the module where the logging was done.

I want to do the same in Glog, in conjunction with also only logging to the standard error, not a file.

How?

UPDATE: By the time control passes to my code, the executable has already been started - I'm running as an AWS Lambda function - so I'm not sure I have the option to set the Glog state on the command line. Can I do it with function calls?


I'm both new to GoLang and I think I just don't understand the internal logic of Go. Because my mind can't penetrate the Glog docs. So this question may seem naive.

  • 写回答

2条回答 默认 最新

  • dongleiqiao2107 2018-06-15 18:12
    关注

    Well, I skimmed the docs and glog is controlled by flags, so they have to be present in order for this to work. In your case, you will have to set them at runtime. You are going to want to set logtostderr=true to log to stderr and set the severity level to one of the possible options:
    stderrthreshold=[WARNING|ERROR|INFO|FATAL]

    Example:

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

    Output:

    $ ./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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。