英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论