Skip to content

Add structured JSON logging for production (Logback + logstash-logback-encoder) #8

Description

@piush365

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

  • logstash-logback-encoder added to pom.xml
  • logback-spring.xml with dev/prod profile split
  • Dockerfile passes -Dspring.profiles.active=prod
  • README documents log format difference per profile

Metadata

Metadata

Assignees

No one assigned

    Labels

    infrastructureDocker, CI/CD, database migrations, configobservabilityMetrics, tracing, logging, dashboards

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions