Go Logrus向contextlogger添加额外字段
我在所有go应用程序中都使用logrus,最近我开始使用上下文记录器。现在我想在我的应用程序的执行路径中“建立”一个上下文。见下面的例子,它说明了我想要什么Go Logrus向contextlogger添加额外字段,go,Go,我在所有go应用程序中都使用logrus,最近我开始使用上下文记录器。现在我想在我的应用程序的执行路径中“建立”一个上下文。见下面的例子,它说明了我想要什么 package main import ( "github.com/Sirupsen/logrus" ) func main() { logrus.Info("normal logger") cl := logrus.WithFields( logrus.Fields{
package main
import (
"github.com/Sirupsen/logrus"
)
func main() {
logrus.Info("normal logger")
cl := logrus.WithFields(
logrus.Fields{
"extra_field_one": "extra_value_one",
})
// some code here
// here I want to add an additional field to to contextlogger cl.
// How do I do that?
}
编辑
如前所述,可以通过覆盖contextLogger来实现。还发现您可以添加一个附加字段:
cl = cl.WithField("key", "value")
您只需调用
cl.WithFields()
输出为:
INFO[0000] hello world extra_field_one="extra_value_one" extra_field_two="extra_value_two"
INFO[0000] hello world extra_field_one="extra_value_one" extra_field_two="extra_value_two"