- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要构建一个logrus自定义格式器,并且Logrus Entry似乎没有一个称为Buffer的字段。
我在用:
go版本go1.13.1 linux / amd64
https://github.com/sirupsen/logrus v0.10.0
Linux Cent OS 7
package util
import (
"bytes"
"encoding/json"
"strings"
"github.com/sirupsen/logrus"
)
type LogFormat struct {
TimestampFormat string
}
func (f *LogFormat) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
b.WriteByte('[')
b.WriteString(strings.ToUpper(entry.Level.String()))
b.WriteString("]:")
b.WriteString(entry.Time.Format(f.TimestampFormat))
if entry.Message != "" {
b.WriteString(" - ")
b.WriteString(entry.Message)
}
if len(entry.Data) > 0 {
b.WriteString(" || ")
}
for key, value := range entry.Data {
b.WriteString(key)
b.WriteByte('=')
b.WriteByte('{')
strValue, _ := json.Marshal(value)
var genericObject interface{}
json.Unmarshal(strValue, &genericObject)
b.WriteString(string(strValue))
b.WriteString("}, ")
}
b.WriteByte('\n')
return b.Bytes(), nil
}
package main
import (
"encoding/json"
"os"
"time"
"git-devops.totvs.com.br/rodrigo.henrique/logrusPeopleExample/data"
"git-devops.totvs.com.br/rodrigo.henrique/logrusPeopleExample/util"
"github.com/sirupsen/logrus"
)
func main() {
carlos := &data.People{
Name: "Carlos",
Age: 18,
LastSeen: time.Now(),
}
pedro := &data.People{
Name: "Pedro",
Age: 22,
Email: "pedro@email.com.br",
LastSeen: time.Now(),
}
json.NewEncoder(os.Stdout).Encode(
&carlos,
)
formatter := util.LogFormat{}
formatter.TimestampFormat = "2006-01-02 15:04:05"
logrus.SetFormatter(&formatter)
logrus.WithFields(logrus.Fields{
"people": &carlos,
}).Info("Logging ", carlos.Name)
logrus.WithFields(logrus.Fields{
"people": &pedro,
}).Info("Logging ", pedro.Name)
logrus.WithFields(logrus.Fields{
"people1": &pedro,
"people2": &carlos,
}).Info("Logging two people")
}
最佳答案
logrus在Buffer
中引入了v0.11.0
。您可以在v0.10.0
之后使用任何版本。
但我建议使用最新版本[v1.4.2
]。他们必须在更高版本中添加了许多功能并解决了问题。
因此,您应该只使用最新版本。
关于go - Logrus条目没有“缓冲区”字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527585/
我正在尝试转换 https://github.com/appscode/voyager从 glide 到 go mod。 我收到如下错误: go: github.com/Sirupsen/logrus
我正在使用 ory-am/hydra,它让我在 Golang 库 gopkg.in/gorethink/gorethink.v3 中出错,错误如下: ../../../gopkg.in/gorethi
logrus是go的一个日志框架,它最让人激动的应该是hook机制,可以在初始化时为logrus添加hook,logrus可以实现各种扩展功能,可以将日志输出到elasticsearch和activ
我正在使用 Logrus,我遇到了在其日志中显示 & 字符的问题: logger.WithFields(logrus.Fields{ "value": "value&value", }
我在我所有的 go 应用程序中都使用 logrus,最近我开始使用上下文记录器。现在我想在我的应用程序的执行路径中“建立”一个上下文。请参见下面的示例,它说明了我想要什么。 package main
我正在尝试从 Golang 日志包转换到 Logrus .我的问题是关于如何自定义记录消息的时间戳格式。默认值是自启动以来的秒数,但我想要“2016-03-24 17:10:15”格式。我的简单测试代
我在 Go 应用程序中使用 logrus。我相信这个问题也适用于任何其他日志记录包(不提供基于外部文件的配置)。 logrus 提供设置各种配置的功能,例如SetOutput、SetLevel 等。
本文主要介绍 Golang 中最佳日志解决方案,包括常用日志包logrus 的基本使用,如何结合file-rotatelogs 包实现日志文件的轮转切割两大话题。 Golang 关于日志处理有很多
免责声明:我已经和 go 一起工作了……大约一天。我在这里是一个真正的n00b... 我想要一个 logrus 记录器,它在 main() 中配置了默认字段,所以我不必指望其他开发人员每次都记录所需的
我正在使用 logrus我的 Go 项目中用于结构化日志记录的库。 我的 logrus 配置如下: // Global variable for logging var gLog = &Logger{
所以我开始使用“logrus”。我将它设置在我自己的记录器包中,如下所示: package logger import( log "github.com/Sirupsen/logru
我在 golang 中使用 logrus 来记录到文件。现在,logrus 正在使用字段名称记录到文件。 即 time="26-03-2017 03:37:58"level=error msg="不是
我使用 Go logrus,我需要更改输出消息的格式。我尝试了几件事都没有成功。 我需要的是使用 logrus 格式化程序这样的格式 [错误]:日期时间 - 消息 我试过没有成功的是这个 l
我正在使用 golang logrus用于记录,我有一个包含所有常规功能的包装器,如 Info(..) , Infof(..) e.t.c 我想实现一个包装函数 Audit(..)用于记录到 sysl
使用包装的 logrus 函数/记录器时,记录器将使用记录器函数调用的文件名和行号作为所有日志行的前缀,例如: INFO[0000]logging.go:39 myfolder/logging.Inf
我有一个 golang 项目,我正在尝试使用 loggly 日志记录,正如他们在 https://www.loggly.com/blog/logging-to-loggly-from-go-with-
我将 Go 与 logrus 一起使用,但我发现时间字段的格式始终为本地时间。如何将 logrus 的时间更改为 UTC 时间? 谢谢 最佳答案 不直接支持时区设置,但您可以使用自定义 log.For
我正在使用 Logrus用于登录我的项目的包。 一切正常,但出于测试目的,我不需要显示 logrus 输出。 我正在查看 logrus.SetOuput 寻找答案。 所以基本上我希望将日志写入/dev
我想要一种方法,我们可以通过这种方法来计算否。 logrus 日志中发生的错误和警告的数量。 我想使用一个结构来记录错误和警告, type ErrorsAndWarnings struct{
这里发生了一些奇怪的事情,我无法弄清楚。 我正在为我的依赖项使用 glide,但有一个包让我很伤心。我添加了用于日志记录的 github.com/sirupsen/logrus 包,然后运行 gl
我是一名优秀的程序员,十分优秀!