Reputation: 521
Using log4j2, with normal textual PatternLayout
, I could include ${hostName} %pid
for displaying hostname and process ID in the log messages.
Using JSONLayout
for collecting messages in JSON objects, I could only make hostname
work, as below
<JSONLayout complete ="true"
compact ="true"
eventEol ="true"
objectMessageAsJsonObject ="true"
locationInfo ="true">
<KeyValuePair key ="hostname"
value ="$${env:hostName}"/>
</JSONLayout>
Tried many different ways but couldn't make the PID show up. Any suggestions, without writing any customized code? Thanks!
Upvotes: 2
Views: 435
Reputation: 521
Debugged the log4j2 source code, version 2.12.1, and this is currently impossible.
The plain PatternLayout uses the LogEventPatternConverter to convert each pattern, for the process id, it uses ProcessIdPatternConverter.
But, JSONLayout took a totally different approach. It uses a set of AbstractLookup implmentations, such as EnvironmentLookup for "env", ContextMapLookup for "ctx". Process id doesn't appear in any of the current lookup classes.
The two layout implementations shared nothing. Ideally, the low-level code should be shared, and patterns supported in one layout should be available in the other.
Upvotes: 3