I need to get a username parameter to my ConsoleAppender in every LoggingEvent (or LogEvent in log4j2).
How can I recreate this implementation of altering the doAppend method of my CustomConsoleAppender in log4j when migrating to log4j2:
public class CustomConsoleAppender extends ConsoleAppender
{
public void doAppend(LoggingEvent event)
{
String username = SecurityContextHelper.getLogonUsername();
if (username != null)
MDC.put("userId", username);
else
MDC.put("userId", "system");
super.doAppend(event);
}
}
The value is then retrieved in the log4j2.properties in the PatternLayout of the corresponding appender by using the pattern: %X{userId}