Skip to content

logging: add StructuredMessage, and ECS trace ids in JSON - #123

Merged
rocketraman merged 5 commits into
masterfrom
rg/structured-logging
Nov 25, 2026
Merged

rocketraman merged 5 commits into
masterfrom
rg/structured-logging

Conversation

@rocketraman

@rocketraman rocketraman commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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.

StructuredMessage

StructuredMessage(text, "order.id" to id, "order.total" to total) is a Log4j2 MapMessage that 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 to null are left out. The message's formatted form (%m, and the JSON message field) 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 as type [id key="value" ...] text. The kdoc explains when to use which.

Breaking changes

  • JSON no longer writes the logging context's trace_id, span_id and trace_flags under those names. It writes the ids as trace.id and span.id.
  • JSON and GCLOUD now also write the entries of any existing map message, such as a StringMapMessage, as top-level fields, beside its message.

Output types

  • JSON keeps Log4j's EcsLayout.json and adds these fields through EventTemplateAdditionalFields:
    • a map message's entries as typed top-level fields (numbers and booleans stay JSON numbers and booleans);
    • the OpenTelemetry context keys trace_id and span_id as ECS's trace.id and span.id. The raw keys and trace_flags are not written: an additional field replaces the template's labels MDC resolver with one that leaves them out. The rest of the logging context stays flattened to top-level fields, as before;
    • the source location as ECS's log.origin.file.name, log.origin.file.line and log.origin.function when LOGGING_LOCATION_ENABLED is set. Log4j's ECS template had no location fields, so the setting did nothing for JSON before.
  • GCLOUD keeps Log4j's GcpLayout.json and adds a map message's entries as top-level fields, through an EventTemplateAdditionalField. Cloud Logging puts them in the entry's jsonPayload.
  • COLOR/DEFAULT and PLAIN append a StructuredMessage's fields after its text, as {order.id=42, order.total=1250}. A new %fields pattern converter writes them and writes nothing for other messages. Log4j's %K would repeat a StringMapMessage's entries, which its %m already prints.

FieldsPatternConverter is written in Java so that Log4j's annotation processor (annotationProcessor(log4j-core)) registers it in Log4j2Plugins.dat and 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: StructuredMessage and the JSON fields, then the ECS trace and span ids, then log.origin, then the text layouts' %fields converter, then the docs and changelog.

Tests and docs

  • StructuredMessageLayoutTest loads each base config, renders events through its Console appender's layout, and checks the output:
    • JSON fields and their types;
    • the trace and span keys in JSON and GCLOUD;
    • ECS error and log.origin fields;
    • that the text layouts print the fields after the text, and print a StringMapMessage's entries only once.
  • The logging docs gain a "Structured fields" section with a compiled snippet. The JSON, GCLOUD and location rows are updated, the telemetry page mentions the new trace keys, and the testing page mentions StructuredMessage.
  • CHANGELOG entry under 3.1.0.

@rocketraman
rocketraman marked this pull request as ready for review November 25, 2026 14:29
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
PR Preview Action v1.8.2
Preview removed because the pull request was closed.
2026-11-25 19:06 UTC

@claude

claude Bot commented Nov 25, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@rocketraman rocketraman changed the title logging: add FieldsMessage, and ECS trace ids in JSON logging: add StructuredMessage, and ECS trace ids in JSON Nov 25, 2026
@rocketraman
rocketraman force-pushed the rg/structured-logging branch 2 times, most recently from 0ce3753 to 780f11d Compare November 25, 2026 17:50
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
rocketraman force-pushed the rg/structured-logging branch from 780f11d to 6f9acbf Compare November 25, 2026 18:29
@rocketraman
rocketraman added this pull request to stack #125 November 25, 2026 18:31
@rocketraman
rocketraman merged commit 6253f4e into master Nov 25, 2026
3 checks passed
@rocketraman
rocketraman deleted the rg/structured-logging branch November 25, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant