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

Unable to programmatically configure additional fields for JsonLayout plugin using LayoutComponentBuilder.

    XMLWordPrintableJSON

Details

    • Question
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.14.1
    • None
    • Layouts
    • None
    • Java 1.8

      Windows 10

    Description

      Hi

      I am unable to programmatically configure the JsonLayout plugin to include custom additional fields. I cannot see a way of doing this using the LayoutComponentBuilder. I have tried various approaches like trying to configure it like other attributes e.g.:

      LayoutComponentBuilder layoutBuilder = builder
      .newLayout("JsonLayout")
      .addAttribute("compact", "true")
      .addAttribute("eventEol", "true")
      .addAttribute("AdditionalField", builder.newKeyValuePair("testfield", "testvalue"));

      However, no matter what name I try and use as a 'key' for additional fields I always get the error

      2021-10-19 14:09:52,121 main ERROR JsonLayout contains an invalid element or attribute "AdditionalField"

      This is easily achievable via the XML configuration e.g.:

      <JSONLayout compact="true" eventEol="true">
         <KeyValuePair key="testfield" value="testvalue"/>
      </JSONLayout>

      The one thing that came to mind is that 'KeyValuePair' is not an attribute of JSONLayout like 'compact' and 'eventEol'. Rather it is a sub-element so using .addAttribute could be where I am going wrong here. However, I cannot see any other obvious methods in LayoutComponentBuilder to achieve this.

      I have been able to use:

      .addAttribute("properties", "true")
      .addAttribute("propertiesAsList", "true")

      This will include any entries added to the ThreadContext map when logging the events which allows me to set variables I need at run time but ideally I don't want to have the outputted JSON including a map or list definition I need to further parse. I need the fields to simply be added as simple variables to the higher level JSON map like everything else and like how it is done when using the XML configuration above.

      Looking at the documentation, I cannot find any examples of how to programmatically configure additional fields for the JSONLayout plugin so I would like to know what I am missing in the part of the API I am using above? I feel that what I am able to achieve via XML configuration should be achievable programmatically using the API.

      Any help or guidance would be much appreciated.

      Thanks

      Mark

       

      Attachments

        Activity

          People

            vy Volkan Yazici
            plasm0r Mark Linley
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: