Logrus:通过配置根据包名设置日志级别。

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

Logrus: Set log level based on package name through configuration

问题

在Java世界中,创建一个与包/类对应的日志记录器是相对常见的做法,类似于:

final static Logger logger = Logger.getLogger("foo.bar.baz");

这意味着我们可以在配置中为不同的包设置不同的日志级别:

log4j.logger.foo.bar=info
log4j.logger.foo.bar.baz=debug

这非常好,因为在一个大型复杂的应用程序中,这意味着你可以消除嘈杂的第三方库的日志输出,或者仅在你认为可能引起问题的包中启用调试日志记录。

我的问题是,logrus是否有类似的功能?如果没有,是否有其他Go日志记录库提供这种功能?

英文:

In the Java world it's relatively common to create one logger per package/class- something like:

final static Logger logger = Logger.getLogger("foo.bar.baz");

This means that in configuration we can set different log levels for different packages:

log4j.logger.foo.bar=info
log4j.logger.foo.bar.baz=debug

This is really nice because in a large, complex, app it means you can silence noisy 3rd party libraries, or alternatively enable debug logging only in the package(s) that you think maybe causing the issue.

My question is whether there is anything similar for logrus and, if there isn't, whether any other go logging libraries offer this sort of functionality.

答案1

得分: 0

是的

使用zap

https://github.com/uber-go/zap


const (
      named = "foo.bar.baz"
)

func main() {
	logger.Named(named).Error("foo")
}

你可以通过包来管理它

英文:

yes

use zap

https://github.com/uber-go/zap


const (
      named = "foo.bar.baz"
)

func main() {
	logger.Named(named).Error("foo")
}

you can manage it by package

huangapple
  • 本文由 发表于 2022年10月29日 18:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/74244637.html
匿名

发表评论

匿名网友

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

确定