Paul Woolley created LOG4J2-2949:
------------------------------------
Summary: Exceptions on parameterised messages when params contain
curly brackets
Key: LOG4J2-2949
URL: https://issues.apache.org/jira/browse/LOG4J2-2949
Project: Log4j 2
Issue Type: Bug
Reporter: Paul Woolley
Attachments: log4j-bug example.zip
I am working with a logging setup that goes Log4j -> Slf4j -> JBoss and
exceptions are being thrown when using parameterised log messages where the
parameter values themselves have curly brackets/braces (`{`) in them.
E.g.:
{code:java}
// LOG is a org.apache.logging.log4j.Logger
LOG.info("Test: {}", "{hi}");
{code}
Results in:
{code:java}
16:27:26,509 org.jboss.logging.LoggerProviders.logProvider:152 (main) DEBUG
logging:152 - Logging Provider:
org.jboss.logging.JBossLogManagerProvider16:27:26,509
org.jboss.logging.LoggerProviders.logProvider:152 (main) DEBUG logging:152 -
Logging Provider: org.jboss.logging.JBossLogManagerProviderLogManager error of
type FORMAT_FAILURE: Formatting errorjava.lang.IllegalArgumentException: can't
parse argument number: hi at
java.text.MessageFormat.makeFormat(MessageFormat.java:1429) at
java.text.MessageFormat.applyPattern(MessageFormat.java:479) at
java.text.MessageFormat.<init>(MessageFormat.java:362) at
java.text.MessageFormat.format(MessageFormat.java:840) at
org.jboss.logmanager.ExtLogRecord.formatRecord(ExtLogRecord.java:508) at
org.jboss.logmanager.ExtLogRecord.getFormattedMessage(ExtLogRecord.java:468) at
org.jboss.logmanager.formatters.Formatters$15.renderRaw(Formatters.java:703) at
org.jboss.logmanager.formatters.Formatters$JustifyingFormatStep.render(Formatters.java:215)
at
org.jboss.logmanager.formatters.MultistepFormatter.format(MultistepFormatter.java:89)
at org.jboss.logmanager.ExtFormatter.format(ExtFormatter.java:32) at
org.jboss.logmanager.handlers.WriterHandler.doPublish(WriterHandler.java:46) at
org.jboss.logmanager.ExtHandler.publish(ExtHandler.java:77) at
org.jboss.logmanager.LoggerNode.publish(LoggerNode.java:333) at
org.jboss.logmanager.LoggerNode.publish(LoggerNode.java:341) at
org.jboss.logmanager.Logger.logRaw(Logger.java:852) at
org.jboss.logmanager.Logger.log(Logger.java:804) at
org.jboss.logging.JBossLogManagerLogger.doLog(JBossLogManagerLogger.java:44) at
org.jboss.logging.Logger.info(Logger.java:1013) at
org.jboss.slf4j.JBossLoggerAdapter.log(JBossLoggerAdapter.java:64) at
org.apache.logging.slf4j.SLF4JLogger.logMessage(SLF4JLogger.java:232) at
org.apache.logging.log4j.spi.AbstractLogger.log(AbstractLogger.java:2110) at
org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2198)
at
org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2152)
at
org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2135)
at
org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2028)
at
org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1899)
at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1444)
at App.main(App.java:9)Caused by: java.lang.NumberFormatException: For input
string: "hi" at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580) at
java.lang.Integer.parseInt(Integer.java:615) at
java.text.MessageFormat.makeFormat(MessageFormat.java:1427) ... 27 more
{code}
I am raising the bug here because I believe the issue stems from the method
`org.apache.logging.slf4j.SLF4JLogger#logMessage` in the `log4j-to-slf4j`
dependency.
The code does:
{code:java}
locationAwareLogger.log(getMarker(marker), fqcn, convertLevel(level),
message.getFormattedMessage(), message.getParameters(), t);
{code}
but `message.getFormattedMessage()` evaluates all the placeholders in the
message. So instead of passing on `"Test: {}"` to the `locationAwareLogger`,
it passes on `"Test: \{hi}"`. Because `message.getParameters()` is also not
empty (it contains the `"\{hi}"` param), the `locationAwareLogger` tries to
evaluate the message all over again and...exception.
Please see the attached sample project, which demonstrates the problem.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)