How do I add fields to log4j2's JSON logs How do I add fields to log4j2's JSON logs json json

How do I add fields to log4j2's JSON logs


This can be archived simply via the the config file.

See my log4j2.yaml:

Configuration:  status: warn  appenders:    Console:      name: STDOUT      JsonLayout:        complete: false        compact: true        eventEol: true        KeyValuePair:          -            key: extrainformation            value: Some very important stuff I need to include          -            key: extrainformation2            value: Some other very important stuff I need to include  Loggers:    Root:      level: "warn"      AppenderRef:        ref: STDOUT

Custom fields are always last, in the order they are declared. The values support lookups


Like @alan7678 said -

A custom layout was also the solution for me.

@Plugin(name = "ExtendedJsonLayout", category = Node.CATEGORY,     elementType = Layout.ELEMENT_TYPE, printObject = true)public class ExtendedJsonLayout extends AbstractJacksonLayout {// Lots of code!}

I created a Log4j2 layout plugin called "extended-jsonlayout". You can include it in your project with Maven or Gradle. Check it out here -

https://github.com/savantly-net/log4j2-extended-jsonlayout