我想使用apache log4j将日志记录添加到我正在开发的应用程序中。此时,我想将所有日志消息重定向到级别INFO和更低(TRACE,DEBUG)到stdout以及所有其他日志消息从WARN及以上(ERROR,FATAL)到stderr。 例如:
...
logger.info("Processing at some point"); // must be written to stdout
logger.debug("Point x was processed"); // must be written to stdout
logger.warn("Incorrect point config"); // must be written only to stderr
logger.error("Exception occurred at point x"); // must be written only to stderr
那么我的log4j.properties文件应该是什么?这是如何看待这个情节:
log4j.rootLogger=DEBUG, stdout, stderr
# configure stdout
# set the conversion pattern of stdout
# Print the date in ISO 8601 format
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = DEBUG
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
# configure stderr
# set the conversion pattern of stdout
# Print the date in ISO 8601 format
log4j.appender.stderr = org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Threshold = WARN
log4j.appender.stderr.Target = System.err
log4j.appender.stderr.layout = org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
上述配置的问题是logger.error()...也在stdout打印。
答案 0 :(得分:46)
以下也以属性文件格式解决。诀窍是让过滤器定义正确。 Log4j properties info here
原始尝试,修改为过滤WARN& ERROR:
log4j.rootLogger=TRACE, stdout, stderr
# configure stdout
# set the conversion pattern of stdout
# Print the date in ISO 8601 format
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = TRACE
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
log4j.appender.stdout.filter.filter1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.stdout.filter.filter1.levelMin=TRACE
log4j.appender.stdout.filter.filter1.levelMax=INFO
# configure stderr
# set the conversion pattern of stdout
# Print the date in ISO 8601 format
log4j.appender.stderr = org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Threshold = WARN
log4j.appender.stderr.Target = System.err
log4j.appender.stderr.layout = org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
答案 1 :(得分:10)
Per Jon Skeet上一篇文章Post
遗憾的是,没有最大阈值,所以无论你到哪里 调试消息,您还会收到警告消息。那有点痛, IMO。
答案 2 :(得分:7)
LevelMax
和LevelMin
。 例如,以下.xml配置定义了两个appender:stdout,stderr。 stdout将所有级别小于或等于INFO的日志打印到stdout,stderr将所有级别大于INFO的日志打印到stderr。
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : log4j.xml
Created on : 13 Δεκέμβριος 2011, 3:55 μμ
Author : elitex
Description : Purpose of the document follows.
-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="stderr" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="warn" />
<param name="target" value="System.err"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d [%t][%F:%L] : %m%n" />
</layout>
</appender>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="debug" />
<param name="target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d [%t][%F:%L] : %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="info" />
</filter>
</appender>
<root>
<priority value="debug"></priority>
<appender-ref ref="stderr" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>
有关详细信息,请查看:http://www.laliluna.de/articles/posts/log4j-tutorial.html
答案 3 :(得分:3)
使用log4j2中的ThresholdFilter
标记的另一个示例。
<Console name="ConsoleERR" target="SYSTEM_ERR">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level - %msg%n"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
请参阅doc。
答案 4 :(得分:0)
这是logback配置,它完全(!)你要求的内容:
{% set button_class = button_class_off|default('toggle toggle-thumbs-down') %}
{% set button_toggle_swap = button_toggle_swap|default(['toggle-thumbs-down', 'toggle-thumbs-up']) %}
{% if value == '1' %}
{% dump(name) %}
{% for swap in button_toggle_swap %}
{% if swap in button_class %}
{% dump(swap) %}
{% dump(button_class) %}
{% set button_class = button_class|replace({swap: ""})|trim %}
{% dump(button_class) %}
{% else %}
{% set button_class = button_class ~ ' ' ~ swap %}
{% endif %}
{% endfor %}
{% endif %}
答案 5 :(得分:0)
将其更改为NEUTRAL并为DEBUG添加过滤器也应使其捕获DEBUG。还有另一个内置过滤器,它需要janino依赖性:
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>
<![CDATA[ level > INFO ]]>
</expression>
</evaluator>
<OnMatch>DENY</OnMatch>
<OnMismatch>NEUTRAL</OnMismatch>
</filter>