Video in progress

Strong Middle 🧩 Microservices

Why do you need structured (JSON) logging, and how do you turn it on in Spring Boot 3.4+?

A log system parses JSON logs without regexes: filtering and aggregation by fields like traceId, level and orderId work directly. Since Boot 3.4 you turn it on with logging.structured.format.console=ecs (or logstash, gelf), with no extra library.

📖 Answer

A plain text log:

2026-11-03 10:15:02.311 ERROR 1 --- [order-service] [nio-8080-exec-7] [6f1c...-a2b9...] c.s.OrderService : Payment failed for order 812

To parse that in Loki or Elasticsearch you write a regex, then someone changes the pattern and the parsing breaks. A multi-line stack trace also gets split into separate records.

In JSON every record is one line with well-defined fields:

logging:
  structured:
    format:
      console: ecs      # Elastic Common Schema; logstash and gelf exist too

To add your own fields, SLF4J's fluent API is handy: the field goes only into this record, without being bound to the MDC:

log.atError()
   .addKeyValue("orderId", order.id())
   .addKeyValue("paymentStatus", status)
   .setCause(ex)
   .log("Payment failed");

As a result the record has @timestamp, log.level, service.name, message, traceId/spanId from the MDC, your orderId and paymentStatus fields, and on an error the stack trace in a separate field. Now "errors in the last hour with paymentStatus=DECLINED" is a single filter.

Before Boot 3.4 the same thing was done with logstash-logback-encoder in logback-spring.xml, and that still works.

Practical advice: human-readable text locally, JSON in prod, separated by profile, because reading JSON in a terminal is unpleasant. And agree on field names across teams: if one service uses orderId and another order_id, searching is still hard.