如何创建两个不同的ROOT Logger并进行logback? [英] How to create 2 different ROOT loggers with logback?
问题描述
我很乐意将SLF4J与logback结合使用,并将 2个附加程序用于 ROOT 记录器.
I'm happily using SLF4J with logback and use 2 appenders for the ROOT logger.
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
我们如何为两个附加程序设置不同的日志级别?我仍然需要所有ROOT-logger消息.
- 调试级别的STDOUT
- INFO 级别的文件
- DEBUG-level for STDOUT
- INFO-level for FILE
所有日志都需要成为输出的一部分(因此需要ROOT记录器).
All log's need to be part of the output (so the ROOT logger is needed).
推荐答案
您将永远不会拥有一个以上的root记录器,因此您的问题有点令人误解.您要寻找的是如何微调每个追加程序记录的事件.
You will not ever have more than one root-logger, so your question is a bit misleading. What you are looking for is how to fine-tune which events each of the appenders does log.
为此,您向每个附加器添加一个ThresholdFilter:
And for that, you add a ThresholdFilter to each of the appenders:
http://logback.qos.ch/manual/filters.html#thresholdFilter
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
为FILE-appender配置级别INFO,为STDOUT配置DEBUG.
Configure level INFO for the FILE-appender and DEBUG for STDOUT.
我不得不质疑另一个答案的错误,即这个答案是错误的:是的,您可以在配置中包含多个根- element .但是,这不会创建多个root- logger ,这就是问题标题所要求的.此外,《登录指南》在 http://logback.qos.ch/manual/configuration下进行了说明. html#syntax (突出显示的内容):
I have to dispute the other answer's charge that this one is wrong: Yes, you can have more than one root-element in the configuration. That does not create more than one root-logger though, which was what the question's title asks for. Also, the logback manual states under http://logback.qos.ch/manual/configuration.html#syntax (highlighting mine):
尽管如此,配置文件的最基本结构仍可以 被描述为<配置>元素,后跟零个或多个 < appender>元素,后跟零个或多个<记录器>元素, 其次是至多一个<根>元素.
Nevertheless, the very basic structure of the configuration file can be described as, < configuration > element, followed by zero or more < appender > elements, followed by zero or more < logger > elements, followed by at most one < root > element.
这可能有用,但至少是违反惯例的.
It might work, but at the very least it's against convention.
这篇关于如何创建两个不同的ROOT Logger并进行logback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!