我想在同一个附加程序中使用两种不同的模式配置 log4j 2。即,只要出现错误,日志文件中就应该出现特定的模式。我不是在尝试两个不同的日志文件,而是在同一日志文件中尝试两种不同的模式。每当出现错误时,我都会看到“MYDOMAINDOTCOM_SUPPORT_NEEDED”,并且该字符串将触发一封自动电子邮件发送给支持团队。
我有以下配置,仅在“RollingFile”附加程序中打印错误消息,并且“RollingFileError”附加程序被忽略。我缺少什么?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">/documents/log</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
<RollingFile name="RollingFileError" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<param name="threshold" value="error" />
<PatternLayout>
<pattern>MYDOMAINDOTCOM_SUPPORT_NEEDED %d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.beans.factory" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug" />
<appender-ref ref="RollingFileError" level="error" />
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
您永远不应该配置两个附加程序来写入同一个文件。拥有两个都使用相同文件并滚动到相同文件模式的滚动文件附加程序永远不会正常工作。
此外,您的配置最终会导致所有错误消息被记录两次;一次是由于其调试级别而使用 RollingFile 附加程序,一次是由于其错误级别而使用 RollingFileError 附加程序。
相反,您应该有一个滚动文件附加程序,并使用 PatternSelector 来决定使用哪种模式。看http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors有关模式选择器的文档。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)