[ 
https://issues.apache.org/jira/browse/LOG4J2-3179?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17434908#comment-17434908
 ] 

Mark Linley edited comment on LOG4J2-3179 at 10/27/21, 3:24 PM:
----------------------------------------------------------------

[~vy] Thanks so much for your help! That did resolve the problem for me.

However, I moved on to using the Elastic {{java-ecs-logging}} layout plugin for 
log4j which ensures your log output is compliant with the ECS format for 
Elastic Search and that's been working nicely for me. After I got this working 
I did see that JsonTemplateLayout has an ECS template to use as well but I 
think I'm going to stick with the official Elastic log4j layout plugin for now.

Which documentation should include API configuration examples?

Thanks

Mark


was (Author: plasm0r):
[~vy] Thanks so much for your help! That did resolve the problem for me.

However, I moved on to using the Elastic {{java-ecs-logging}} layout plugin for 
log4j which ensures your log output is compliant with the ECS format for 
Elastic Search and that's been working nicely for me. After I got this working 
I did see that JsonTemplateLayout has an ECS template to use as well but I 
think I'm going to stick with the official elastic log4j layout plugin for now.

Which documentation should include API configuration examples?

Thanks

Mark

> Unable to programmatically configure 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
>            Assignee: Volkan Yazici
>            Priority: Major
>             Fix For: 2.13.3, 2.14.1
>
>
> 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 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