Background
The current logging pattern outputs plain text. In a production environment running inside containers (ECS, Kubernetes), logs are ingested by tools like Datadog, Loki, or CloudWatch Logs Insights that work far better with structured JSON — each field is individually queryable without regex parsing.
Proposed Change
Add logstash-logback-encoder and a logback-spring.xml with profile-based configuration:
- dev profile: Human-readable console output (current behaviour).
- prod profile: JSON output with all MDC fields (
correlationId, traceId) as top-level JSON keys.
Dependency
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.4</version>
</dependency>
logback-spring.xml (prod appender)
<springProfile name="prod">
<appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeMdcKeyName>correlationId</includeMdcKeyName>
</encoder>
</appender>
<root level="INFO"><appender-ref ref="JSON"/></root>
</springProfile>
Example JSON output
{"@timestamp":"2024-11-15T10:30:01.234Z","level":"INFO","correlationId":"f47a...","message":"Order accepted","logger":"FlashSaleService"}
Acceptance Criteria
Background
The current logging pattern outputs plain text. In a production environment running inside containers (ECS, Kubernetes), logs are ingested by tools like Datadog, Loki, or CloudWatch Logs Insights that work far better with structured JSON — each field is individually queryable without regex parsing.
Proposed Change
Add
logstash-logback-encoderand alogback-spring.xmlwith profile-based configuration:correlationId,traceId) as top-level JSON keys.Dependency
logback-spring.xml (prod appender)
Example JSON output
{"@timestamp":"2024-11-15T10:30:01.234Z","level":"INFO","correlationId":"f47a...","message":"Order accepted","logger":"FlashSaleService"}Acceptance Criteria
logstash-logback-encoderadded topom.xmllogback-spring.xmlwith dev/prod profile split-Dspring.profiles.active=prod