Skip to content

logging: add structuredMessageOf, for a serializable value's fields - #124

Merged
rocketraman merged 1 commit into
masterfrom
rg/structured-message-serialization
Nov 25, 2026
Merged

rocketraman merged 1 commit into
masterfrom
rg/structured-message-serialization

Conversation

@rocketraman

Copy link
Copy Markdown
Owner

Stacked on #123, which adds StructuredMessage.

A new module, boot-logging-log4j2-serialization, adds structuredMessageOf. It makes a StructuredMessage from a @Serializable value, with each of the value's properties as a field:

@Serializable
data class Refund(val orderId: String, val amount: Long, val reason: String)

log.info(structuredMessageOf("order ${refund.orderId} refunded", refund, prefix = "refund"))
// fields: refund.orderId, refund.amount, refund.reason
  • The value is encoded to a JsonElement with kotlinx.serialization's Json, or with a json the caller passes, so @SerialName and the Json configuration apply.
  • A nested object's properties become fields named with a dot, such as refund.customer.id.
  • Numbers and booleans keep their types, a list stays a list (which JSON writes as an array), and a null is left out.
  • prefix is optional. A value that isn't encoded as an object, such as a number, is a single field named by the prefix, which it then requires.
  • There are two overloads: a reified one, and one that takes a SerializationStrategy.

It is a separate module so that boot-logging-log4j2 doesn't bring kotlinx-serialization-json to every application.

Tests and docs

  • StructuredMessageOfTest checks the fields, their names and types, the prefix, a non-object value, and the output through the JSON layout.
  • The logging page gets a "From a serializable value" section, with a compiled snippet. The module is added to modules.yml as an optional group, and to the CHANGELOG.

@rocketraman
rocketraman added this pull request to stack #125 November 25, 2026 18:31
@rocketraman
rocketraman force-pushed the rg/structured-message-serialization branch from dcc7ace to 18a28f1 Compare November 25, 2026 18:32
@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:23 UTC

Base automatically changed from rg/structured-logging to master November 25, 2026 19:05
boot-logging-log4j2-serialization's structuredMessageOf encodes a
@serializable value with kotlinx.serialization and makes a
StructuredMessage with each property as a field. A nested object's
properties are named with a dot, numbers and booleans keep their
types, lists stay lists, and nulls are left out. An optional prefix
starts each field's name.

It is its own module, so that boot-logging-log4j2 doesn't bring
kotlinx-serialization-json to every application.
@rocketraman
rocketraman force-pushed the rg/structured-message-serialization branch from 18a28f1 to 09473bd Compare November 25, 2026 19:06
@rocketraman
rocketraman marked this pull request as ready for review November 25, 2026 19:07
@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 merged commit b934703 into master Nov 25, 2026
4 checks passed
@rocketraman
rocketraman deleted the rg/structured-message-serialization 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