Logback:如何仅将错误记录到文件 [英] Logback: how to log only errors to file
问题描述
我已经阅读了两个小时的注销手册,但仍然不知道该怎么做.
I've been reading the logback manual for 2 hours and still can't figure how to do what I need.
这就像标题所说的一样简单:我只想将错误记录到文件中,而其他级别(包括ERROR)要记录到控制台.
It is as simple as the title says: I want to log only the errors to a file, and the other levels (including ERROR) to console.
这是我的logcat.xml文件的根部分:
This is the root section of my logcat.xml file:
<root level="TRACE" >
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" />
</root>
此配置的问题在于,它将每个级别> = TRACE都记录到两个附加程序中.
The problem with this configuration is that it logs every level >= TRACE to both appenders.
我可以只使用控制台作为根目录,并定义一个文件记录器:
I could let the root with only console, and define a file logger:
<logger name='file_logger' level='ERROR' >
<appender-ref ref="FILE_APPENDER" />
</logger>
但是然后我必须像这样调用普通的记录器:
But then I'd have to call the normal logger like this:
LoggerFactory.getLogger(ClientClass.class);
文件记录器是这样的:
LoggerFactory.getLogger("file_logger");
我不想为每个班级选择记录仪.我只想使用类作为参数从工厂获取根记录程序,并根据级别进行正确的操作.
I don't wan't to choose the logger for each class. I just want to get the root logger from the factory using the class as parameter, and have it do the correct thing depending on the level.
这可能吗?
推荐答案
将其放入文件追加程序定义中:
Put this into your file appender definition:
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
ThresholdFilter
在logback-classic.jar
中.
这篇关于Logback:如何仅将错误记录到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!