本文介绍了如何使用Log4j2 xml重写appender在登录文件之前修改LogEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在log4j2.xml文件中使用Rewrite appender,以便在记录之前我可以修改日志。我没有得到谷歌的帮助。根据log4j2文件,Rewrite是一个具有重写方法的接口,而MapRewritePolicy是实现类,当我运行它时,我能够看到我的web3.log文件生成但没有看到日志内容的任何修改。我在项目中看到了MapRewritePolicy源代码并将MapRewritePolicyImpl.java创建为本地实现类,并将一些System.out用于查看代码流是否从log4j2.xml文件进入此类。我已经修改了我的log4j2.xml以使用MapRewritePolicyImpl.java但是代码流没有进入我的MapRewritePolicyImpl.java类。

I want to use Rewrite appender in my log4j2.xml file so that before logging I can modify logs. I have not get much helps from google. As per log4j2 documents Rewrite is an interface has rewrite method and MapRewritePolicy is implementation class, when I run this I am able to see my web3.log file generating but not seeing any modification in log content. I seen MapRewritePolicy source code and created local implementation class as MapRewritePolicyImpl.java in my project and put some System.out to see code flow is coming into this class from log4j2.xml file. I have modified my log4j2.xml to use MapRewritePolicyImpl.java but code flow is not going into my MapRewritePolicyImpl.java class.

<Rewrite name="rewrite" >
            <Appender-Ref ref="web3" />
             <MapRewritePolicyImpl">
                <KeyValuePair key="creditCard" value="new12345"/>
            </MapRewritePolicyImpl> 
        </Rewrite>



    <Configuration monitorInterval="5" status="debug" strict="true">
        <Appenders>
            <RollingFile name="web3" fileName="../logs/web3.log" 
                filePattern="${sys:catalina.home}/logs/$${date:yyyy-MM-dd}/web3-%d{yyyy-MM-dd}-%i.log.gz">
                <PatternLayout
                    pattern="%d{dd/MM/yyyy HH:mm:ss,SSS} [%X{cartID}] [%X{sessionId}] [%p] [%t] [%c] (%F:%L)  - %m%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1"
                        modulate="true" />
                    <SizeBasedTriggeringPolicy size="10 MB" />

                </Policies>
            </RollingFile>
            <Rewrite name="rewrite" >
                <Appender-Ref ref="web3" />
                 <MapRewritePolicy">
                    <KeyValuePair key="creditCard" value="new12345"/>
                </MapRewritePolicy> 
            </Rewrite>
    </Appenders>
    <Loggers>
        <Logger name="com.virginamerica" level="info" additivity="false">
            <!-- <Appender-Ref ref="web3" /> -->
            <Appender-Ref ref="rewrite"/>
        </Logger>
    </Loggers>
</Configuration>


推荐答案

MapRewritePolicy将评估包含MapMessage的LogEvents,并将添加或更新Map的元素。这仅适用于您的应用程序调用 logger.info(new MapMessage(keyValueMap)) .I怀疑这不是您的应用程序当前正在做的事情。

MapRewritePolicy will evaluate LogEvents that contain a MapMessage and will add or update elements of the Map. This only works if your application calls logger.info(new MapMessage(keyValueMap)). I suspect that this is not what your application is currently doing.

通常,您的消息将是SimpleMessage(如果您调用 logger.info (只是一个没有参数的字符串))或一个ParameterizedMessage(如果你调用 logger.info(Hi {}!,名称) )。 RewriteAppender将无法对SimpleMessage或ParameterizedMessages执行任何操作,因此不会替换任何内容...

Usually, your messages will be either a SimpleMessage (if you called logger.info("Just a string without parameters")) or a ParameterizedMessage (if you called logger.info("Hi {}!", name)). RewriteAppender will not be able to do anything with either SimpleMessage or ParameterizedMessages, so nothing is replaced...

您可能需要查看:这有能力替换字符串消息中的正则表达式一些替换值,如果您使用替换{pattern} {regex} {substitution} 模式。

You may want to take a look at the documentation for PatternLayout: this has the capability to replace regular expressions in string messages with some replacement value, if you use the replace{pattern}{regex}{substitution} pattern.

这篇关于如何使用Log4j2 xml重写appender在登录文件之前修改LogEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:28