问题描述
我有一个正常的 INFO 级别的应用程序日志.我需要的是另外记录所有错误级别的事件以分隔错误日志.我正在使用这样的配置:
I have a normal INFO level log for application. What I need is to additionally log all ERROR level events to separate error log. I am using configuration like this:
<logger name="com.acme">
<level value="error"/>
<appender-ref ref="error"/>
</logger>
<logger name="com.acme">
<level value="info"/>
</logger>
<root>
<level value="warn"/>
<appender-ref ref="general"/>
</root>
此配置仅记录错误.如果我首先放置信息级别记录器,那么它只会记录到一般的附加程序,但错误记录器将不起作用.我想让他们都工作.
This configuration logs errors only. If I put info level logger first, then it will log only to general appender, but error logger will not work. I would like to have them both working.
推荐答案
你需要做的是有一个单独的 定义,定义级别为 INFO,但在你的两个 appender 中定义,您相应地设置它们的阈值,例如
What you need to do is have a single <logger>
definition with a defined level of INFO, but in your two appender definitions, you set their thresholds accordingly, e.g.
<appender name="ERROR_FILE">
<param name="Threshold" value="ERROR"/>
</appender>
<appender name="GENERAL">
<param name="Threshold" value="INFO"/>
</appender>
然后将两个 appender 添加到记录器中:
You then add both appenders to your logger:
<logger name="com.acme">
<level value="INFO"/>
<appender-ref ref="ERROR_FILE"/>
<appender-ref ref="GENERAL"/>
</logger>
现在进入记录器的日志条目将被发送到两个 appender,但由于它们具有不同的独立阈值,ERROR_FILE appender 只会记录 ERROR 及以上.
Log entries now going to the logger will get sent to both appenders, but since they have different independent thresholds, the ERROR_FILE appender will only log ERROR and above.
这篇关于如何配置 log4j 以将不同的日志级别记录到同一记录器的不同文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!