Alv123
Alv123

Reputation: 451

Use custom parameters in JSON Layout [Log4j 2]

I'm confused about the meaning of property substitution, lookups and layout parameters in Log4j 2. The documentation mentions that JSON layout supports custom fields. However it doesn't seem to support conversion patterns like %d{ISO8601}, %m, %l and the like. it does however support Lookups.

Thus when I define in the xml:

            <JsonLayout complete="false" compact="false">
                <KeyValuePair key="@timestamp" value="%d{ISO8601}" />
                <KeyValuePair key="message" value="%message" />
                <KeyValuePair key="process.thread.name" value="%tn" />
            </JsonLayout >

As output I simply get the strings %d{ISO8601}, %message... instead of the values.

What I'm trying to achieve is a JSON layout where I can include parameters similar to Pattern Layout where I simply write <pattern>%d %p %C{1.} [%t] %m%n</pattern> to get what I want. Or, alternatively, should I use the Pattern layout and stitch together a string in JSON Format, making use of the Pattern Layout's JSON encoding %enc{%m}{JSON}?

Upvotes: 0

Views: 679

Answers (1)

rgoers
rgoers

Reputation: 9151

The GelfLayout currently supports a messagePattern attribute that will format just the message field in the JSON using the patternLayout. I have planned to add this to the JSONLayout as well but have not done it yet. There is a new JsonTemplateLayout that is in the final stages of being merged into Log4j 2 that will also support this. You could either work from the current pull request to get the Layout or wait for the Log4j 2.14.0 release when likely both options will be available.

Upvotes: 1

Related Questions