A JSON log format makes events machine-readable, but field naming, correlation and data limits remain application contracts.
Spring Boot structured logs: stable fields without customer data
Format is only the first decision
Spring Boot can emit structured JSON on the console by selecting a supported format such as ecs. The result is easier for a collector to parse than a changing text pattern, but it does not decide which business fields belong in logs. Record event names, bounded outcomes and trace identifiers. Keep raw receipt contents, access tokens and customer contact details out of both message text and structured key-value pairs. Trace correlation joins a request across services; it does not authorize payload logging.
Preserve a small schema
Choose one service name, one environment name and stable keys for receipt action and outcome. A high-cardinality receipt ID may be acceptable in a controlled audit record, but shipping it on every debug line changes retention and access requirements. Keep the log schema versioned in the collector contract so dashboards do not break when a field is renamed. If the application supplies custom Logback or Log4j configuration, verify that it respects Boot's structured-format setting rather than assuming the property overrides a custom appender.
Test one event end to end
Generate a successful receipt issue, a rejected request and a downstream timeout. Parse the emitted lines as JSON, assert required fields and inspect every value for secret or personal-data leakage. Check collector ingestion and search by trace ID. A logger-level change can multiply line volume without changing code; runtime logger control therefore needs a bounded incident window. Metric labels should stay much narrower than searchable log fields.
Implementation contract
spring.application.name=receipt-api
logging.structured.format.console=ecs
logging.structured.ecs.service.environment=${APP_ENV:development}Cost and verification
JSON encoding adds bytes and formatting work per event. Collector storage cost scales with event volume and retained fields; debug logging across thousands of requests can dominate the expense far more than the formatter itself.
Common Mistakes
- Do not treat JSON formatting as permission to emit tokens or customer payloads.
- Do not rename fields without checking collector queries and dashboards.
- Do not assume custom logging configuration automatically honors the Boot format property.
Read next
Spring tracing and logs: preserve request identity across outbound calls, Spring Actuator loggers endpoint: temporary diagnosis with guarded write access, Spring Boot metrics: bound tag values instead of tracking each receipt, Spring Prometheus scrape endpoint: registry, exposure and label contract, Spring Boot config source priority: a builder default may lose to a packaged file.
