rgoers commented on PR #2419:
URL: https://github.com/apache/logging-log4j2/pull/2419#issuecomment-2024398297
@rocketraman I did look into that and what you are asking for just can't be
implemented in a reasonable way without significant mods to the API and will
likely impact performance. The problem is that Loggers are singletons and the
code where the LogEvent is constructed and the ContextMap is populated is
pretty far down the stack. We defer creating LogEvents to avoid the performance
overhead when they aren't going to be used. Messages, on the other hand, are
constructed very early as they are fairly light-weight. So doing that in a
per-instance Logger that wraps the singleton was fairly easy to do.
You will need to point me to some docs on Google Cloud Logging, however at
my employer we are running a cloud infrastructure and log to an ELK stack and
making this work the way you would like would be fairly easy, although it might
be nice to enhance JsonTemplateLayout if it makes sense. To give you an idea,
in my infrastructure whe have JsonTemplateLayout configured iwth
```
{
"@timestamp": {
"$resolver": "timestamp",
"pattern": {
"format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"timeZone": "UTC"
}
},
"ecs.version": "1.11.0",
"log.level": {
"$resolver": "level",
"field": "name"
},
"application": "${lower:${spring:spring.application.name}}",
"short_message": {
"$resolver": "message",
"stringified": true
},
"message": {
"$resolver": "pattern",
"pattern": "[%t] %X{requestId, sessionId, loginId, ipAddress}
%C{1.}.%M:%L - %m%n"
},
"process.thread.name": {
"$resolver": "thread",
"field": "name"
},
"log.logger": {
"$resolver": "logger",
"field": "name"
},
"event.action": {
"$resolver": "marker",
"field": "name"
},
"event.data": {
"$resolver": "map",
"stringified": true
},
"labels": {
"$resolver": "mdc",
"flatten": true,
"stringified": true,
"pattern": "(?!(?i:token)).+"
},
"tags": {
"$resolver": "ndc"
},
"error.type": {
"$resolver": "exception",
"field": "className"
},
"error.message": {
"$resolver": "exception",
"field": "message"
},
"error.stack_trace": {
"$resolver": "exception",
"field": "stackTrace",
"stackTrace": {
"stringified": true
}
}
}
```
Note that with this configuration the "message" key will look exactly like
it would in a log file. However all the context fields, except for the user's
token, are included as distinct fields while all the message map items are
under the event.data tag. However, these could have also been flattened into
distinct fields as the context data is and you wouldn't know the difference
when searching. If you have other requirements for how the data should be
formatted I am sure JsonTemplateLayout could be enhanced to support it, if it
can't do it already.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]