How to avoid only messages and not all lines in log4j?

I have the following PatternLayout:

public class EscapedEnhancedPatternLayout extends EnhancedPatternLayout { @Override public String format(LoggingEvent event) { return StringEscapeUtils.escapeJava(super.format(event)); } } 

but this eludes the full line of logging.

I want to have something like this, but only for the message.

but the LoggingEvent class does not have setMessage and setRenderedMessage .

And I do not see the copy constructor in LoggingEvent . If the LoggingEvent has a copy constructor, I can inherit from the LoggingEvent and override the methods mentioned below.

Please tell me how to solve my problem.

+5
source share
2 answers

You are right, there is no LoggingEvent(LoggingEvent other) constructor LoggingEvent(LoggingEvent other) , but you can pass event values ​​to the LoggingEvent constructor in your format method similar to this:

 @Override public String format(LoggingEvent event) { Object msgObj = event.getMessage(); LoggingEvent newEvent = new LoggingEvent( event.getFQNOfLoggerClass(), event.getLogger(), event.getTimeStamp(), event.getLevel(), StringEscapeUtils.escapeJava(msgObj != null ? msgObj.toString() : null), event.getThreadName(), event.getThrowableInformation(), event.getNDC(), event.getLocationInformation(), event.getProperties()); return super.format(newEvent); } 

This will create a new LoggingEvent from the old with all values ​​set. Now the StringEscapeUtils.escapeJava method can change the message without affecting other properties, and you can still use super.format

0
source

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 { /** * Creates a log event. * * @param loggerName The name of the Logger. * @param marker An optional Marker. * @param fqcn The fully qualified class name of the caller. * @param level The event Level. * @param data The Message. * @param properties Properties to be added to the log event. * @param t An optional Throwable. * @return The LogEvent. */ @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); } } 
0
source

Source: https://habr.com/ru/post/1247141/


All Articles