如何配置 Logback 将记录器的不同级别记录到不同的目的地?
如何配置 Logback 将记录器的不同级别记录到不同的目的地?
例如,给定以下 Logback 配置,Logback 会将 INFO
消息记录到 STDOUT
并将 ERROR
消息记录到 STDERR
吗?
(请注意,此示例是 第 3 章:Logback 配置)。
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="STDERR"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
<target>System.err</target>
</appender>
<!-- What is the effective level of "chapters.configuration"? -->
<logger name="chapters.configuration" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="chapters.configuration" level="ERROR" additivity="false">
<appender-ref ref="STDERR" />
</logger>
<!-- turn OFF all logging (children can override) -->
<root level="OFF">
<appender-ref ref="STDOUT" />
</root>
</configuration>
How can I configure Logback to log different levels for a logger to different destinations?
For example, given the following Logback configuration, will Logback record INFO
messages to STDOUT
and ERROR
messages to STDERR
?
(Note that this example is a variation of example logback-examples/src/main/java/chapters/configuration/sample4.xml
shown in Chapter 3: Logback Configuration).
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="STDERR"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
<target>System.err</target>
</appender>
<!-- What is the effective level of "chapters.configuration"? -->
<logger name="chapters.configuration" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="chapters.configuration" level="ERROR" additivity="false">
<appender-ref ref="STDERR" />
</logger>
<!-- turn OFF all logging (children can override) -->
<root level="OFF">
<appender-ref ref="STDOUT" />
</root>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这是我使用的配置,工作正常,它基于 XML + JaninoEventEvaluator (需要 Janino 库添加到类路径)
This is the configuration that I use, which works fine, it is based on XML + JaninoEventEvaluator (requires the Janino library to be added to Classpath)
我使用 logback.groovy 来配置我的 logback,但您也可以使用 xml 配置来完成:
我认为使用 GEventEvaluator 更简单,因为不需要创建过滤器类。
我为我的英语道歉!
I use logback.groovy to configure my logback but you can do it with xml config as well:
I think to use GEventEvaluator is simplier because there is no need to create filter classes.
I apologize for my English!
我不相信这个答案,因为它只是上面最好的两个答案的组合:X. Wo Satuk 的答案和 Sébastien Helbert 的答案:
ThresholdFilter
很可爱,但你不能将其配置为具有较高级别和较低级别*,但将其与两个设置为“DENY”WARN
和ERROR
的LevelFilters
结合起来会很不错。非常重要:不要忘记 STDERR 附加程序中的
System.err
标记:我的遗漏让我沮丧了几分钟。* 但是,它在 API 但我不知道你在这种情况下如何使用它。
I take no credit for this answer, as it's merely a combination of the best two answers above: that of X. Wo Satuk and that of Sébastien Helbert:
ThresholdFilter
is lovely but you can't configure it to have an upper level as well as a lower level*, but combining it with twoLevelFilters
set to "DENY"WARN
andERROR
works a treat.Very important: do not forget the
<target>System.err</target>
tag in the STDERR appender: my omission of it had me frustrated for a few minutes.* it does however have a method
decide
in the API but I haven't a clue how you'd use it in this context.试试这个。您可以只使用内置的
ThresholdFilter
和LevelFilter
。无需以编程方式创建自己的过滤器。在此示例中,WARN 和 ERROR 级别记录到 System.err,其余级别记录到 System.out:Try this. You can just use built-in
ThresholdFilter
andLevelFilter
. No need to create your own filters programmically. In this example WARN and ERROR levels are logged to System.err and rest to System.out:无需编程。配置让您的生活变得轻松。
以下是将不同级别的日志记录到不同文件的配置
No programming needed. configuration make your life easy.
Below is the configuration which logs different level of logs to different files
如何将级别为“INFO”或更高级别的彩色消息输出到控制台以及将级别为“WARN”或更高级别的消息输出到文件的示例。
您的 logback.xml 文件:
Example of how to output colored messages of level "INFO" or higher to console and messages of level "WARN" or higher to file.
Your logback.xml file:
我相信这将是最简单的解决方案:
I believe this would be the simplest solution:
更新:有关使用 Groovy 的所有基于配置的方法,请参阅 Dean Hiller 的回答。
--
您可以使用 Logback 过滤器 做一些有趣的事情。下面的配置只会将警告和错误消息打印到 stderr,并将其他所有内容打印到 stdout。
logback.xml
com.foo.StdOutFilter
com.foo.ErrOutFilter
Update: For an all configuration based approach using Groovy see Dean Hiller's answer.
--
You can do some interesting things with Logback filters. The below configuration will only print warn and error messages to stderr, and everything else to stdout.
logback.xml
com.foo.StdOutFilter
com.foo.ErrOutFilter
仅基于配置的解决方案,使用 ThresoldFilter 和 LevelFilters 使事情非常易于理解:
Solution based on configuration only, with a ThresoldFilter and LevelFilters to keep things really simple to understand :
好的,这是我最喜欢的 xml 方式。我为 Eclipse 版本执行此操作,这样我可以
,并且由于某种原因所以没有正确显示这一切,但大多数似乎都在那里...
okay, here is my favorite xml way of doing it. I do this for the eclipse version so I can
and for some reason SO is not showing this all properly but most seems to be there...
最简单的解决方案是在附加程序上使用
ThresholdFilter
:完整示例:
更新:正如 Mike 在评论中指出的那样,具有 ERROR 级别的消息在此处打印到 STDOUT 和 STDERR。但不确定 OP 的意图是什么。如果这不是您想要的,您可以尝试迈克的答案。
The simplest solution is to use
ThresholdFilter
on the appenders:Full example:
Update: As Mike pointed out in the comment, messages with ERROR level are printed here both to STDOUT and STDERR. Not sure what was the OP's intent, though. You can try Mike's answer if this is not what you wanted.