Apache Log4j2 is used for printing info, error, debug, trace logging statements in enterprise applications.
Inorder to format all the log messages externally before printing in logs, we need to create Filters in Log4j 1.x versions.
In Log4j2 , we can use LogEventPatternConverter to filter/convert/format log messages before printing in the log files in server.
e.g., we can filter out any PII data format such as card numbers, dates, any particular string format before printed in the logs., so that the PII data can be effectively maintained.,(not disclosed)
Lets see how we can configure the filters/converters in Log4j2
The following versions are being used for below example to configure Log4j2.
- Java 1.5+
- Maven
- Log4j Version 2.17
Create a simple Maven project with Log4j2 like in previous tutorial
Once the SetUp is done,
Please include the following the Plugin Java File to implement our filter/converter changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.tutorialflow.example.config; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.pattern.ConverterKeys; import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; import org.apache.logging.log4j.core.pattern.PatternConverter; @Plugin(name = "DataPatternConverter", category = PatternConverter.CATEGORY) @ConverterKeys({"filteredlogmessage"}) public class DataPatternConverter extends LogEventPatternConverter { protected DataPatternConverter(String name, String style) { super(name, style); } public DataPatternConverter(String[] options) { super("filteredlogmessage", "filteredlogmessage"); } public static DataPatternConverter newInstance(String[] options) { return new DataPatternConverter(options); } /** * Capture the log message before printing to the console/log file * Modify the log message/filter the log message for any characters.. */ @Override public void format(LogEvent event, StringBuilder toAppendTo) { String rawLogMessage = event.getMessage().getFormattedMessage(); String filteredMessage = rawLogMessage.replaceAll("\\d{6}", ""); toAppendTo.append(filteredMessage); } } |
In the above Java class., we are replacing all log messages containing 6 digit numbers with empty string.
Hence all 6 digits numbers will not be printed in log message. e.g., to avoid printing any cards, serials etc.,
- @Plugin – Annotation of Log4j2 to identify this class as a Plugin
- @ConverterKeys – Annotation to define the key which can be used in the log4j2.properties / xml file to print the filtered message.
Log4j2.properties will be like below.
1 2 3 4 5 6 7 8 9 10 11 |
status = warn name = Log4j2PluginDemo packages = com.tutorialflow.example.config appender.console.type = Console appender.console.name = consoleLogger appender.console.layout.type = PatternLayout appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %filteredlogmessage %n rootLogger.level = debug rootLogger.appenderRef.stdout.ref = consoleLogger |
In the above log4j2 properties, we can see instead of just printing the log message %m we are printing %filteredlogmessage, which will print the log messages excluding the 6 digit numbers
[DataPatternConverter.format() method will be called for each log message before beign printed]
Also note the package name of plugin is included in log4j properties
packages = com.tutorialflow.example.config
Finally in the application, in the below code we are printing 6 digits numbers along with the log messages, however it will be removed before printing in the logs., as explained above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.tutorialflow.example; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4j2PluginLayoutExample { private static Logger logger = LogManager.getLogger(Log4j2PluginLayoutExample.class); public static void main(String[] args) { logger.info("Log4j2Example Info 82348 421347 Message Printed "); logger.debug("Log4j2Example Debug 123456789 Message Printed"); logger.error("Log4j2Example Error 123123 1234534524 Message Printed"); } } |
The resulting logs will be printed like below.
1 2 3 |
2021-12-28 21:39:57 INFO Log4j2PluginLayoutExample:12 - Log4j2Example Info 82348 Message Printed 2021-12-28 21:39:57 DEBUG Log4j2PluginLayoutExample:14 - Log4j2Example Debug 789 Message Printed 2021-12-28 21:39:57 ERROR Log4j2PluginLayoutExample:16 - Log4j2Example Error 4524 Message Printed |
The complete Log4j2 Converter Project is available in the following GitHub Location.,