Mark Linley created LOG4J2-3179:
-----------------------------------

             Summary: Unable to programmatically configuring additional fields 
for JsonLayout plugin using LayoutComponentBuilder.
                 Key: LOG4J2-3179
                 URL: https://issues.apache.org/jira/browse/LOG4J2-3179
             Project: Log4j 2
          Issue Type: Improvement
          Components: Layouts
    Affects Versions: 2.14.1
         Environment: Java 1.8

Windows 10
            Reporter: Mark Linley
             Fix For: 2.14.1, 2.13.3


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.:
{quote}LayoutComponentBuilder layoutBuilder = builder
 .newLayout("JsonLayout")
 .addAttribute("compact", "true")
 .addAttribute("eventEol", "true")
 .addAttribute("AdditionalField", builder.newKeyValuePair("testfield", 
"testvalue"));
{quote}
However, no matter what name I try and use as a 'key' for additional fields I 
always get the error
{quote}2021-10-19 14:09:52,121 main ERROR JsonLayout contains an invalid 
element or attribute "AdditionalField"
{quote}
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:
{quote}.addAttribute("properties", "true")
 .addAttribute("propertiesAsList", "true")
{quote}
This will include any entries added to the ThreadContext map when logging the 
events which allows me to set variables I need to 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

 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to