Repository navigation
logging: add StructuredMessage, and ECS trace ids in JSON - #123
Merged
Merged
Conversation
rocketraman
marked this pull request as ready for review
November 25, 2026 14:29
|
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
rocketraman
force-pushed
the
rg/structured-logging
branch
2 times, most recently
from
November 25, 2026 17:50
0ce3753 to
780f11d
Compare
A StructuredMessage is a Log4j map message with a human-readable text and typed fields. Its message is the text alone. JSON and GCLOUD flatten a map message's entries into top-level fields, so that numbers and booleans stay JSON numbers and booleans. JSON names Log4j's EcsLayout.json template, its default, explicitly.
JSON wrote the OpenTelemetry context keys trace_id, span_id and trace_flags as top-level fields of those names. Write trace_id and span_id as the Elastic Common Schema's trace.id and span.id instead, and replace the template's labels field with one that leaves the three keys out of the flattened logging context.
Log4j's ECS template has no location fields, so JSON wrote none even with LOGGING_LOCATION_ENABLED set. Add log.origin.file.name, log.origin.file.line and log.origin.function.
Add a %fields pattern converter, which writes a StructuredMessage's
fields as {key=value, ...} and nothing for other messages, and append
it to the text layouts' message. Log4j's %K would repeat the entries
of a StringMapMessage, whose %m already writes them.
The converter is in Java, so that Log4j's plugin annotation processor
registers it and generates its GraalVM metadata; the formatting is in
Kotlin.
Add a Structured fields section to the logging page, with a snippet, describe the JSON and GCLOUD fields, and note the changes in the changelog, including the breaking ones.
rocketraman
force-pushed
the
rg/structured-logging
branch
from
November 25, 2026 18:29
780f11d to
6f9acbf
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Applications that log structured events have each had to write their own map message and JSON template to get their fields indexed as top-level fields. This PR adds both to
boot-logging-log4j2.StructuredMessageStructuredMessage(text, "order.id" to id, "order.total" to total)is a Log4j2MapMessagethat holds a human-readable text and typed fields. You can log it directly, or subclass it for an event that is logged in many places. Fields set tonullare left out. The message's formatted form (%m, and the JSONmessagefield) is the text alone.It differs from Log4j's
StructuredDataMessage, which is for syslog's RFC 5424 structured data: that needs an id and a type, limits keys to 32 ASCII characters, holds only strings, and formats its message astype [id key="value" ...] text. The kdoc explains when to use which.Breaking changes
JSONno longer writes the logging context'strace_id,span_idandtrace_flagsunder those names. It writes the ids astrace.idandspan.id.JSONandGCLOUDnow also write the entries of any existing map message, such as aStringMapMessage, as top-level fields, beside itsmessage.Output types
JSONkeeps Log4j'sEcsLayout.jsonand adds these fields throughEventTemplateAdditionalFields:trace_idandspan_idas ECS'strace.idandspan.id. The raw keys andtrace_flagsare not written: an additional field replaces the template'slabelsMDC resolver with one that leaves them out. The rest of the logging context stays flattened to top-level fields, as before;log.origin.file.name,log.origin.file.lineandlog.origin.functionwhenLOGGING_LOCATION_ENABLEDis set. Log4j's ECS template had no location fields, so the setting did nothing forJSONbefore.GCLOUDkeeps Log4j'sGcpLayout.jsonand adds a map message's entries as top-level fields, through anEventTemplateAdditionalField. Cloud Logging puts them in the entry'sjsonPayload.COLOR/DEFAULTandPLAINappend aStructuredMessage's fields after its text, as{order.id=42, order.total=1250}. A new%fieldspattern converter writes them and writes nothing for other messages. Log4j's%Kwould repeat aStringMapMessage's entries, which its%malready prints.FieldsPatternConverteris written in Java so that Log4j's annotation processor (annotationProcessor(log4j-core)) registers it inLog4j2Plugins.datand generates its GraalVM reflection metadata. It calls a Kotlin function,formatFields, to write the fields.Commits
Each commit builds and passes the module's tests on its own:
StructuredMessageand the JSON fields, then the ECS trace and span ids, thenlog.origin, then the text layouts'%fieldsconverter, then the docs and changelog.Tests and docs
StructuredMessageLayoutTestloads each base config, renders events through itsConsoleappender's layout, and checks the output:JSONandGCLOUD;log.originfields;StringMapMessage's entries only once.JSON,GCLOUDand location rows are updated, the telemetry page mentions the new trace keys, and the testing page mentionsStructuredMessage.