Open hdevalke opened 2 years ago
Another option is to bypass log4j to create object messages using a custom message factory and use -Dlog4j2.messageFactory=CustomMessageFactory
. But I think a configuration property is a nicer solution.
public class CustomMessageFactory extends AbstractMessageFactory {
@Override
public Message newMessage(String message, Object... params) {
return new ParameterizedMessage(message, params);
}
@Override
public Message newMessage(Object message) {
return new ParameterizedMessage("{}", message);
}
}
Log4j2
ObjectMessage
s are automatically converted to json objects. This makes the log statements fail, if the object cannot be converted to json or it generates invalid json.I would propose a
objectMessageAsJsonObject
config. Putting it to false would just put the objectstoString()
result into themessage
field, as described in the documentation of log4j2.e.g.
produces:
This patch would probably enough to implement this feature: