How to mask log4j2 log messages

I am using log4j2 (version - 2.5) and I am trying to write a message converter plugin that will mask some of the well-known log message templates.

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

When I launch my web application with this plugin, I see this warning

WARN. The converter key 'm' is already mapped to the class org.apache.logging.log4j.core.pattern.MessagePatternConverter. Sorry, Dave, I can't let you do this! Ignoring the plugin [class MyCustomFilteringLayout].

After exploring the log4j2 site, I found these links.

Link

If several converters specify the same ConverterKeys files, then loading the order above determines which one will be used. For example, to override the% date converter, which is provided by the built-in DatePatternConverter, you need to place your plug-in in the JAR file in CLASSPATH before log4j-core.jar. It is not recommended; Config Converter Image Code will result in a warning to emit. Try using unique ConverterKeys files for your template converters.

I need help to understand how I can write my own converters for m / msg. Is there a better way to do this?

Additional Information: I created a shaded can for MyCustomFilteringLayout. The reason I do this is because I want the masking logic to be separate from the application.


Update

I created a converter for my own key, which looks like this:

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

Here I can not write another converter for the same ConverterKeys - cm? Now my log4j2.xml has this template,

 <PatternLayout> <Pattern>%d %p %c{1.} [%t] %cm %ex%n</Pattern> </PatternLayout> 
+5
source share
1 answer

Your update solves the problem and answers the question of how to replace the built-in message converter with a custom one. He needs a unique key.

It looks like you want to parameterize your template. Many templates accept the options parameter. You can use this to control behavior, so specifying% cm {key1} in the layout template will give different results than% cm {key2}.

For an example of a converter that accepts parameters, see the source code for MdcPatternConverter .

+3
source

All Articles