Video in progress

Staff 🌱 Spring

Log volume and cost have shot up, yet during an incident you still can't find the information you need. How do you rebuild the log strategy for the platform?

I make logs structured (JSON) with common fields, set clear rules for levels, remove noise or sample it, mask sensitive data, and set retention by log type. And for many questions, metrics and traces answer better than logs.

📖 Answer

These two problems usually come from one cause: logs are written "in case they're needed" but not designed for searching.

  1. Analysis. Which loggers produce most of the volume? Usually 5–10 places: the full request/response body on every request, DEBUG inside loops, logs of health-check requests, repeated stack traces. Cleaning these up often cuts the volume several times over.

  2. A structured format. In Spring Boot 3.4+ it is built in:

logging:
  structured:
    format:
      console: ecs        # or logstash, gelf

Common fields in every record: service, version, env, traceId, spanId, tenant, userId (an ID, not a name). Then "all services' logs for this trace" is one query, not a regex.

  1. Agreement on levels:
  • ERROR: someone needs to act; can be tied to an alert.
  • WARN: unexpected, but the system recovered on its own.
  • INFO: business events and state changes, one or two lines per request.
  • DEBUG: off by default in production; if needed, switched on temporarily for one package through /actuator/loggers.

An error is logged in one place only: "log and rethrow" writes the same stack trace 4 times across the layers.

  1. Cost control.
  • Sample the access log of successful requests, but keep errors in full.
  • Retention by type: debug/access 7 days, business audit separately, for years, in cheap storage.
  • A per-service log budget and a volume dashboard, to see who increased it.
  1. Security. Passwords, tokens, card numbers, passport data must not reach the log. Masking at the encoder level plus a code review rule; sensitive fields are left out of DTOs' toString().

  2. The right tool. "How many requests?", "what's the p99?" should not be computed from logs; metrics are cheaper and faster. "Where did the request slow down?" is a trace. Logs are for the details of a specific event.

I measure the result: volume and cost, and the time it takes to find the needed information in the next incident.