- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用包装的 logrus 函数/记录器时,记录器将使用记录器函数调用的文件名和行号作为所有日志行的前缀,例如:
INFO[0000]logging.go:39 myfolder/logging.Info()
如果我像这样包装 log 函数,例如:
import (
"fmt"
"github.com/sirupsen/logrus"
"os"
"path"
"runtime"
)
var (
log *logrus.Logger
)
func init() {
log = logrus.New()
log.SetReportCaller(true)
log.Formatter = &logrus.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
},
}
}
func Info(args ...interface{}) {
log.Info(args...)
}
此函数发出的每一行都将以
logging function
的行号为前缀。称呼。这是预期的,但所需的行为是每行都以行的行号为前缀,其中
Info
叫做。
INFO[0000]myfile.go:39 myfolder/myfile.myfunction()
有什么办法可以解决吗?
最佳答案
在 logrus 中是不可能做到的。我有一个类似的要求,最终做了以下对我们有用的事情。
package mylog
import (
"fmt"
"github.com/Sirupsen/logrus"
"runtime"
"strings"
)
var logger = logrus.New()
func SetLogFormatter(formatter logrus.Formatter) {
logger.Formatter = formatter
}
// Info logs a message at level Info on the standard logger.
func Info(args ...interface{}) {
if logger.Level >= logrus.InfoLevel {
entry := logger.WithFields(logrus.Fields{})
entry.Data["file"] = fileInfo(2)
entry.Info(args...)
}
}
func fileInfo(skip int) string {
_, file, line, ok := runtime.Caller(skip)
if !ok {
file = "<???>"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
return fmt.Sprintf("%s:%d", file, line)
}
看看这个或它的一些变体是否适用于您的用例。我已经从上面的代码片段中删除了特定于应用程序的代码。
关于go - 是否可以在不丢失行号前缀的情况下包装 logrus.Logger 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63658002/
我正在尝试转换 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
我是一名优秀的程序员,十分优秀!