An alternative approach might be to create a custom LogEventFactory . A LogEventFactory used to create LogEvents. Applications can replace the standard LogEventFactory by setting the value of the Log4jLogEventFactory system property to the name of the custom LogEventFactory class.
The method that will be called to create the LogEvent instance is as follows:
LogEvent createEvent(String loggerName, org.apache.logging.log4j.Marker marker, String fqcn, org.apache.logging.log4j.Level level, org.apache.logging.log4j.message.Message data, List<Property> properties, Throwable t)
and DefaultLogEventFactory is the main implementation for log4j. In your case, you can extend the base implementation and avoid the message, after which you call the default implementation. It will be something like this:
public class MyCustomLogEventFactory extends DefaultLogEventFactory { @Override public LogEvent createEvent(final String loggerName, final Marker marker, final String fqcn, final Level level, final Message data, final List<Property> properties, final Throwable t) { super.createEvent(loggerName, marker, fqcn, level, StringEscapeUtils.escapeJava(data != null ? data.toString() : null), properties, t); } }
source share