简体   繁体   English

如何屏蔽log4j2日志消息

[英]How to mask log4j2 log messages

I am using log4j2(version - 2.5) and I am trying write a message converter plugin which will mask some of the know patterns of the log message. 我正在使用log4j2(版本-2.5),我正在尝试编写一个消息转换器插件,它将掩盖日志消息的一些已知模式。

@Plugin(name = "CustomeMasking",
        category = "Converter")
@ConverterKeys({"m"})
public class MyCustomFilteringLayout extends LogEventPatternConverter {
}

When I run my web application with this plugin then I see this warn message 当我使用此插件运行我的Web应用程序时,我看到此警告消息

WARN Converter key 'm' is already mapped to 'class org.apache.logging.log4j.core.pattern.MessagePatternConverter'. WARN转换器键'm'已映射到'class org.apache.logging.log4j.core.pattern.MessagePatternConverter'。 Sorry, Dave, I can't let you do that! 对不起,戴夫,我不能让你那样做! Ignoring plugin [class MyCustomFilteringLayout]. 忽略插件[类MyCustomFilteringLayout]。

After exploring log4j2 site I have found these references. 在探索log4j2网站后,我找到了这些参考资料。

Reference 参考

If multiple Converters specify the same ConverterKeys, then the load order above determines which one will be used. 如果多个转换器指定相同的ConverterKeys,则上面的加载顺序将确定将使用哪个。 For example, to override the %date converter which is provided by the built-in DatePatternConverter class, you would need to place your plugin in a JAR file in the CLASSPATH ahead of log4j-core.jar. 例如,要覆盖由内置DatePatternConverter类提供的%date转换器,您需要将插件放在log4j-core.jar之前的CLASSPATH中的JAR文件中。 This is not recommended; 不推荐这样做; pattern ConverterKeys collisions will cause a warning to be emitted. 模式ConverterKeys冲突将导致发出警告。 Try to use unique ConverterKeys for your custom pattern converters. 尝试将独特的ConverterKeys用于自定义模式转换器。

I need help to understand how can I write my custom converters for m/msg. 我需要帮助才能理解如何为m / msg编写自定义转换器。 Is there any better way to do it? 有没有更好的方法呢?

Additional Details: I have created shaded jar for MyCustomFilteringLayout. 其他细节:我为MyCustomFilteringLayout创建了阴影jar。 Reason why I am doing this way is that I want to keep masking logic separate from application. 我这样做的原因是我希望将屏蔽逻辑与应用程序分开。


Updated 更新

I have created converter for my own key which looks like this, 我为自己的密钥创建了转换器,看起来像这样,

@Plugin(name = "CustomeMasking",
            category = "Converter")
    @ConverterKeys({"cm"})
    public class MyCustomFilteringLayout extends LogEventPatternConverter {
    }

Here I can't write another converter for same ConverterKeys - cm? 在这里,我不能为同一个ConverterKeys写入另一个转换器 - 厘米? Now my log4j2.xml has this pattern layout, 现在我的log4j2.xml有这个模式布局,

<PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %cm %ex%n</Pattern>
            </PatternLayout> 

Your update solves the problem and answers the question how to replace the built-in message converter with a custom one. 您的更新解决了该问题,并回答了如何使用自定义消息转换器替换内置消息转换器的问题。 It needs a unique key. 它需要一个唯一的密钥。

Sounds like you want to parameterize your pattern. 听起来你想要参数化你的模式。 Many patterns take an options parameter. 许多模式采用options参数。 You can use this to control behavior, so specifying %cm{key1} in your layout pattern will produce different results than %cm{key2}. 您可以使用它来控制行为,因此在布局模式中指定%cm {key1}将产生与%cm {key2}不同的结果。

For an example of a converter that takes parameters, see the source code of the MdcPatternConverter . 有关带参数的转换器示例,请参阅MdcPatternConverter的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM