在slf4j后面使用Log4j2时,是否可以使用String.format语法格式化日志消息?

似乎标准MessageFactoryParameterizedMessageFactory,仅支持{}

最佳答案

您不能像使用Log4j API那样在每个Logger的基础上更改MessageFactory。但是,可以通过将log4j2.messageFactory设置为要使用的MessageFactory类名称来更改默认的MessageFactory。您可以将其指定为系统属性,也可以在名为log4j2.component.properties的文件中定义该属性,该文件位于类路径中。

我建议您可能要考虑使用FormattedMessageFactory,因为它支持String.format模式,MessageFormat模式和ParameterizedMessage模式(尽管不在同一条消息中)。

10-08 16:24