logrus是标准go log.Logger的包装器。

huangapple go评论66阅读模式
英文:

logrus wrapper for standard go log.Logger

问题

我有一个结构体,请求一个标准的log.Logger作为日志消息的参数。从zap logger中,可以通过NewStdLogAt提供一个包装器:

stdLogAt, err := zap.NewStdLogAt(&s.log, zap.ErrorLevel)

现在我必须在logrus日志记录器环境中使用这个模块。我没有找到logruslog.Logger提供的实现。我有哪些可能的方法?是否有可用的实现?

英文:

I have a struct requesting a standard log.Logger as parameter for log messages. From the zap logger a wrapper is provided by NewStdLogAt:

stdLogAt, err := zap.NewStdLogAt(&s.log, zap.ErrorLevel)

Now I have to use this module in a logrus logger environment. I have not found logrus providing an implementation for log.Logger. What are my possible approaches? Is there an implementation available?

答案1

得分: 1

Logrus文档似乎建议使用Writer函数的结果来创建一个新的log.Logger,Logrus的loggersentries提供了这个函数。

w := logrusLogger.Writer()

// 或者定义一个错误级别:
// logrusLogger.WriterLevel(logrus.ErrorLevel)

// 注意你需要负责关闭写入器
defer w.Close() 

stdlibLogger := log.New(w, "", 0)

我从这个页面的"Logger as an io.Writer"部分推断出这一点。

英文:

The Logrus documentation seems to suggest creating a new log.Logger using the result of the Writer function which Logrus loggers and entries provide.

w := logrusLogger.Writer()

// or to define an error level:
// logrusLogger.WriterLevel(logrus.ErrorLevel)

// note that you are responsible for closing the writer
defer w.Close() 

stdlibLogger := log.New(w, "", 0)

I have inferred this from the "Logger as an io.Writer" section on this page.

huangapple
  • 本文由 发表于 2023年5月20日 03:37:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76291992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定