Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2949

Exceptions on parameterised messages when params contain curly brackets

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Cannot Reproduce
    • None
    • None
    • None
    • None

    Description

      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.:

      // LOG is a org.apache.logging.log4j.Logger
      LOG.info("Test: {}", "{hi}");
      

      Results in:

      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
      

      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:

      locationAwareLogger.log(getMarker(marker), fqcn, convertLevel(level), message.getFormattedMessage(), message.getParameters(), t);
      

      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.

      Attachments

        1. log4j-bug example.zip
          5 kB
          Paul Woolley

        Activity

          People

            Unassigned Unassigned
            pwoolley Paul Woolley
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: