Add Graylog Extended Log Format (GELF) for structured logging

See gh-42158
This commit is contained in:
Samuel Lissner
2024-08-26 17:14:21 +02:00
committed by Moritz Halbritter
parent 915fc2e4f9
commit 74a9d11d1b
12 changed files with 747 additions and 2 deletions

View File

@@ -445,6 +445,7 @@ Structured logging is a technique where the log output is written in a well-defi
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.gelf[Graylog Extended Log Format (GELF)]
* xref:#features.logging.structured.logstash[Logstash]
To enable structured logging, set the property configprop:logging.structured.format.console[] (for console output) or configprop:logging.structured.format.file[] (for file output) to the id of the format you want to use.
@@ -492,6 +493,53 @@ logging:
NOTE: configprop:logging.structured.ecs.service.name[] will default to configprop:spring.application.name[] if not specified.
NOTE: configprop:logging.structured.ecs.service.version[] will default to configprop:spring.application.version[] if not specified.
[[features.logging.structured.gelf]]
=== Graylog Extended Log Format (GELF)
https://go2docs.graylog.org/current/getting_in_log_data/gelf.html[Graylog Extended Log Format] is a JSON based logging format for the Graylog log analytics platform.
To enable the Graylog Extended Log Format, set the appropriate `format` property to `gelf`:
[configprops,yaml]
----
logging:
structured:
format:
console: gelf
file: gelf
----
A log line looks like this:
[source,json]
----
{"version":"1.1","short_message":"Hello structured logging!","timestamp":1.725530750186E9,"level":6,"_level_name":"INFO","_process_pid":9086,"_process_thread_name":"main","host":"spring-boot-gelf","_log_logger":"com.slissner.springbootgelf.ExampleLogger","_userId":"1","_testkey_testmessage":"test"}
----
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.gelf.service` properties:
[configprops,yaml]
----
logging:
structured:
gelf:
service:
name: MyService
version: 1.0
environment: Production
node-name: Primary
----
NOTE: configprop:logging.structured.gelf.service.name[] will default to configprop:spring.application.name[] if not specified.
NOTE: configprop:logging.structured.gelf.service.version[] will default to configprop:spring.application.version[] if not specified.
[[features.logging.structured.logstash]]