Refine structured logging

Refine structured logging to support `Environment`, `ApplicationPid` and
`ElasticCommonSchemaService` injection. With these updates we are able
to remove the `ApplicationMetadata` class and simplify the parameters
passed to the layout/encoder classes.

Closes gh-41491
This commit is contained in:
Phillip Webb
2024-07-24 17:59:45 +01:00
parent 50dbaec2d0
commit de3b14f2b4
30 changed files with 539 additions and 290 deletions

View File

@@ -440,7 +440,7 @@ Handling authenticated request
== Structured Logging
Structured logging is a technique where the log output is written in a well-defined, often machine-readable format.
Spring Boot supports structured logging and has support for the following formats out of the box:
Spring Boot supports structured logging and has support for the following JSON formats out of the box:
* xref:#features.logging.structured.ecs[Elastic Common Schema (ECS)]
* xref:#features.logging.structured.logstash[Logstash]
@@ -474,6 +474,22 @@ A log line looks like this:
This format also adds every key value pair contained in the MDC to the JSON object.
You can also use the https://www.slf4j.org/manual.html#fluent[SLF4J fluent logging API] to add key value pairs to the logged JSON object with the https://www.slf4j.org/apidocs/org/slf4j/spi/LoggingEventBuilder.html#addKeyValue(java.lang.String,java.lang.Object)[addKeyValue] method.
The `service` values can be customized using `logging.structured.ecs.service` properties:
[configprops,yaml]
----
logging:
structured:
ecs:
service:
name: MyService
version: 1.0
environment: Production
node-name: Primary
----
NOTE: configprop:logging.structured.ecs.service.name[] will default to configprop:spring.application.name[] if not specified.
[[features.logging.structured.logstash]]