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

JsonLayout logs malformed if toString() contains empty curly braces {}

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 2.11.0
    • Fix Version/s: None
    • Component/s: Appenders
    • Labels:
      None

      Description

      When using JsonLayout for our appender, an object that has "{}" in its toString will result in a recursive output.  Here is a simple code example and resulting output to illustrate the problem:

      import org.apache.logging.log4j.LogManager;
      import org.apache.logging.log4j.Logger;
      
      public class Test {
      
          public static final Logger log = LogManager.getLogger(Test.class);
      
          public static void main(final String[] args) {
              log.info("Testing {}", new TestObj());
          }
      
          static class TestObj {
              @Override
              public String toString() {
                  return "This is my toString {} with curly braces";
              }
          }
      }
      

      With this log4j configuration:

      <Configuration status="INFO">
      	<Appenders>
      		<Console
      			name="Console"
      			target="SYSTEM_OUT">
      			<JsonLayout
      				properties="true"
      				complete="true" />
      		</Console>
      	</Appenders>
      	<Loggers>
      		<Root level="info">
      			<AppenderRef ref="Console" />
      		</Root>
      	</Loggers>
      </Configuration>
      

       Produces the following output:

      [
      {
        "thread" : "main",
        "level" : "INFO",
        "loggerName" : "Test",
        "message" : "Testing This is my toString This is my toString {} with curly braces with curly braces",
        "endOfBatch" : false,
        "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
        "instant" : {
          "epochSecond" : 1527636451,
          "nanoOfSecond" : 604000000
        },
        "contextMap" : { },
        "threadId" : 1,
        "threadPriority" : 5
      }
      ]
      

      Note how TestObj's toString() is inserted in place of its "{}", resulting in it being logged twice in a malformed fashion.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                husby024 Joseph Husby
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: