简体   繁体   中英

How to use formatted message in Log4j2 with slf4j

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM