Is it possible to format a log message with the syntax from String.format
when Log4j2 is used behind slf4j?
It seems the standard MessageFactory
is ParameterizedMessageFactory
which supports only {}
.
You cannot change the MessageFactory on a per Logger basis as you can with the Log4j API. However, you can change the default MessageFactory by setting log4j2.messageFactory to the MessageFactory class name you want to use. You can either specify it as a system property or you can define the property in a file named log4j2.component.properties that can be found in the classpath.
I would suggest you might want to consider using the FormattedMessageFactory as it supports String.format patterns, MessageFormat patterns and ParameterizedMessage patterns (not in the same message though).
You can't do that without accessing the native logger, but that would be messy and not very useful, in the end. See this answer if you really want to do that.