我想为不同的消息级别设置一个具有不同布局的appender。我提出的所有解决方案都需要两个不同的appender,如:
<Appender type="Console" name="STDOUT_D">
<Layout type="PatternLayout" pattern="[%-5p] %d %c - %m%n"/>
<Filters>
<ThresholdFilter level="fatal" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
</Filters>
</Appender>
<Appender type="Console" name="STDOUT">
<Layout type="PatternLayout" pattern="[%-5p] %d %-60.60c - %m%n"/>
<Filters>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</Appender>
想法?
我正在使用带有严格模式的XML配置。
答案 0 :(得分:0)
我使用ScriptPatternSelector
取得了这个结果<Appender type="Console" name="STDOUT">
<PatternLayout>
<ScriptPatternSelector defaultPattern="[%-5level] %c{1.} %C{1.}.%M.%L %msg%n">
<Script name="selector" language="groovy"><![CDATA[
if (logEvent.getLevel() == org.apache.logging.log4j.Level.DEBUG)
return "DEBUG"
else
return "INFO"
]]>
</Script>
<PatternMatch key="DEBUG" pattern="[%-5p] %d %c - %m%n"/>
<PatternMatch key="INFO" pattern="[%-5p] %d %-60.60c - %m%n"/>
</ScriptPatternSelector>
</PatternLayout>
</Appender>