4

在 Java(我通常使用的语言)中,日志库的一个共同特点是能够设置“全局”日志级别 -例如,如果级别为WARNING或更高,则发送到日志文件,否则不会,无论完成日志记录的模块。

我想在 Glog 中做同样的事情,同时只记录标准错误,而不是文件。

如何?

更新:当控制权传递给我的代码时,可执行文件已经启动——我作为 AWS Lambda 函数运行——所以我不确定我是否可以选择在命令行上设置 Glog 状态。我可以用函数调用来做吗?


我都是 GoLang 的新手,我想我只是不了解 Go 的内部逻辑。因为我的思想无法穿透Glog 文档。所以这个问题可能看起来很幼稚。

4

3 回答 3

6

好吧,我浏览了文档,并且 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
于 2018-06-15T18:12:12.773 回答
1

据我所知,它们不提供您正在寻找的功能。然而,这似乎是您可以很容易地使用适配器模式的东西。

我在这里为您创建了一个准系统实现:https: //play.golang.org/p/LJPWwF0gTgB

Although, I suggest you look into another logging library that provides something more like what you're wanting. I personally use https://github.com/uber-go/zap, as it also ties nicely into opentracing via Jaeger, which is something I generally need in addition to writing logs.

I think zap might feel more correct, but I also don't have context on your use case of have any issues with glog. Just a preference that might help.

于 2018-06-15T19:14:12.520 回答
1

Kubernetes klog is a fork of glog which solves some annoying issues like global Flag setting

https://github.com/kubernetes/klog

package main

import (
    "flag"
    "fmt"
    "k8s.io/klog"
)

func main() {
    klog.InitFlags(nil)
    flag.Set("logtostderr", "true")
    flag.Set("stderrthreshold", "WARNING")
    flag.Set("v", "2")
    flag.Parse()

    number_of_lines := 10

    for i := 0; i < number_of_lines; i++ {
        klog.V(2).Infof("LINE: %d", i)
        message := fmt.Sprintf("TEST LINE: %d", i)
        klog.Warning(message)
    }
    klog.Flush()
}
于 2019-10-26T22:20:50.083 回答