Skip to content

BP-69: Print slog context attrs in log4j2; add OTel JSON example#4770

Open
merlimat wants to merge 3 commits intoapache:masterfrom
merlimat:bp-69-log4j-context-attrs
Open

BP-69: Print slog context attrs in log4j2; add OTel JSON example#4770
merlimat wants to merge 3 commits intoapache:masterfrom
merlimat:bp-69-log4j-context-attrs

Conversation

@merlimat
Copy link
Copy Markdown
Contributor

Summary

  • Slog attributes are forwarded to log4j2 as ContextData (the same channel SLF4J's MDC uses). Add %X to every PatternLayout in the user-facing log4j2 configs (conf/, bookkeeper-server/src/main/resources/, stream/conf/, bookkeeper-benchmark/conf/) so that the structured attrs attached via .attr(...) show up in log output by default.
  • Drop the now-redundant *MDC variant appenders in conf/log4j2.xml and the directentrylogger override that targeted them — every appender renders the context map now.
  • Add conf/log4j2.otel-json.xml plus conf/otel-log-template.json as an example layout that emits each event as a single JSON object aligned with the OpenTelemetry log data model: timestamp, severity_text, severity_number, body, instrumentation_scope.name, resource.{service.name,host.name}, attributes (the full ContextData map), trace_id/span_id/trace_flags from MDC, plus exception.{type,message,stack_trace}.
  • The OTel example uses JsonTemplateLayout, which requires org.apache.logging.log4j:log4j-layout-template-json on the classpath at runtime (not bundled by default — the example header notes this).

Test plan

  • xmllint --noout validates every modified XML config.
  • python3 -m json.tool validates conf/otel-log-template.json.
  • Confirm via existing test runs that the log4j2 configuration still loads cleanly.

Slog attributes are forwarded to log4j2 as ContextData (the same channel
SLF4J's MDC uses). Add %X to every PatternLayout in the user-facing
log4j2 configs so the structured attributes attached via .attr(...)
appear in log output by default. Drop the now-redundant *MDC variant
appenders and the directentrylogger override that targeted them.

Also add conf/log4j2.otel-json.xml plus conf/otel-log-template.json as
an example layout that emits each event as a JSON object aligned with
the OpenTelemetry log data model (severity_text, severity_number, body,
attributes, resource, instrumentation_scope, trace_id/span_id from MDC).
The example uses log4j-layout-template-json, which must be added to the
classpath alongside log4j-core to enable JsonTemplateLayout.
Replace the previous template with the structure used by Pulsar
(apache/pulsar/blob/master/conf/OtelLogLayout.json):
  * Top-level fields use the spec's PascalCase naming (Timestamp,
    SeverityText, Body, TraceId, SpanId, TraceFlags, Attributes).
  * thread.name, thread.id, code.namespace, exception.{type,message,
    stacktrace}, and the flattened MDC are placed under "Attributes",
    using OTel semantic-convention keys.
  * Drop SeverityNumber, ObservedTimestamp, InstrumentationScope, and
    Resource — the first two are derivable / redundant for app-emitted
    logs, the logger name is "code.namespace" not InstrumentationScope,
    and Resource is normally attached by the collector pipeline.
  * The MDC resolver with flatten=true now sits inside Attributes, so
    slog .attr(...) keys merge as siblings of thread.name etc. (the
    previous placement at the top level didn't merge into "attributes"
    as intended).

Update the explanatory comment block and drop the now-unused
otel.service.name / otel.host.name properties.
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