Quick revision
The short answer
Structured logs are machine-parseable: each event is a JSON object with named fields, so log platforms can search and aggregate them without fragile regular expressions that break when a pattern changes. Every event should carry a timestamp, level, logger, message, service, version, trace id and span id. Business identifiers such as an order id go in as separate key-value fields rather than only inside the message, and field names stay the same across services, for example by following the ECS schema.
What to remember
In this answer, we saw that structured logs are worth the move because they are machine-parseable: no regular expressions, no broken multi-line events, and every detail is a field we can filter and group. Each event should carry timestamp, level, logger, message, service, version, trace id and span id, most of them supplied by configuration and tracing rather than by developers. Business identifiers belong in key-value fields with constant messages, and field names must be consistent across services, which adopting ECS and a small shared dictionary achieves.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Structured logs are machine-parseable: each event is a JSON object with named fields, so log platforms can search and aggregate them without fragile regular expressions that break when a pattern changes. Every event should carry a timestamp, level, logger, message, service, version, trace id and span id. Business identifiers such as an order id go in as separate key-value fields rather than only inside the message, and field names stay the same across services, for example by following the ECS schema.
Why this question is asked
Most teams start with text logs because they are readable on a laptop. The trouble begins at scale: dozens of services, a central log platform, and an incident at 3 a.m. when someone needs every line about one order across five services. With text logs that search depends on patterns and luck; with structured logs it is a single query. Interviewers ask this design question to see whether we understand the reasons behind the move, and whether we can name the fields that make logs useful across a whole system. In this answer, we will compare the two approaches, see how parsing text goes wrong, choose the required fields with the reason for each, show how business fields should be added, and explain why consistent names matter as much as the format.
1A Quick Recap: Text Logs Versus Structured Logs
A text log line is produced from a pattern. Spring Boot's default looks like a date, a level, a process id, a thread name, a logger name and a message, separated by spaces and brackets. A structured log line contains the same information as a JSON object, where every piece has a name.
Notice that the structured version separates what happened (the message) from the details (order id, error type, timeout). Each detail is a field we can filter, count or group by.
2Why Parsing Text Is Fragile
To search text logs by level or logger, the log platform must split each line with a pattern, usually a regular expression. That works until something changes. One team adds the application name to its pattern, and every field shifts by one position. A message contains a line break, as stack traces do, and one event becomes forty lines that the platform treats as separate events. A logger name gets abbreviated differently. Each of these breaks dashboards and alerts silently: nothing errors, the numbers are just wrong.
Business details are worse. In the text line above, the order id is inside the message. Finding all failures for that order means a full-text search for A-88213, which also matches unrelated lines, and counting failures by error type means writing another expression to cut the type out of free text. With structured logs, the platform does none of this guessing; it reads the fields.
| Question | Text logs | Structured logs |
|---|---|---|
| All errors in orders | Regex on level and logger | service.name:orders AND log.level:ERROR |
| Every line of one request | Hope the trace id is in the pattern | trace.id:7c1e9b0f4a2d... |
| Failures for order A-88213 | Full-text search, noisy | order.id:A-88213 |
| Errors by version during a rollout | Usually impossible | Group by service.version |
| Multi-line stack traces | Split into many events | One event, error.stack_trace field |
3Choosing the Required Fields
Every field on the required list must earn its place by answering a question we ask during incidents. The timestamp, in UTC with milliseconds, orders events from different machines. The level separates failures from normal events and drives alerts. The logger, the full class name, tells us which part of the code wrote it. The message says what happened, in a short, stable sentence.
The next three describe where the event came from. The service name lets us filter to one service among a hundred. The version lets us compare the old and new builds during a rollout and tells us whether a fix is deployed. The environment is useful too, so test and production never mix in a search.
The last two connect logs across services. The trace id is shared by every span of one request as it travels through services, so searching for it returns the whole story of that request in order. The span id identifies the step within that request, which lets us link a log line to the exact span in the tracing tool. In Spring Boot, Micrometer Tracing puts both into the MDC for the duration of each request, so they appear on every line without extra code.
Check your version for the exact property and field names; the point is that every one of these fields is supplied by configuration and the platform, not typed by developers in each log call.
4Adding Business Identifiers as Fields
Developers naturally put identifiers inside the message: "Payment failed for order " + id. That makes the message unique for every order, which defeats grouping, and hides the id in free text. The better habit is a short, constant message plus key-value arguments. With SLF4J 2's fluent API, each key-value pair becomes its own field in the structured output.
Now a dashboard can count Payment failed events grouped by payment.provider, and a search for one order finds exactly its lines. For values that apply to a whole request, such as a tenant id, putting them in the MDC once at the start of the request adds them to every line in that request. One caution applies: fields are searchable and kept for weeks, so personal data and secrets must stay out of them just as they stay out of messages.
5Consistent Field Names Across Services
Structured logs only pay off fully when every service uses the same names. If one service writes traceId, another trace_id and a third trace.id, every cross-service query needs all three, and someone will forget one. The simplest way to avoid that is to adopt an existing schema instead of inventing one. The Elastic Common Schema, ECS, defines names for the common fields (log.level, service.name, trace.id, error.type and many more), and Spring Boot's ecs format already uses them. For our own business fields, a short shared dictionary, such as order.id, customer.id, payment.provider, written down once and used everywhere, does the same job. A platform starter that sets the format and a lint check in tests that rejects unknown top-level field names keep it consistent.
- Developer logs a constant message
- Adds business ids as key-value fieldsnames from the shared dictionary
- Formatter adds timestamp, level, loggerECS names
- Configuration adds service, version, environment
- MDC adds trace id and span idfrom Micrometer Tracing
- Platform indexes every fieldsame query works for every service
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Do structured logs make local development harder?
They are harder to read raw, so many teams keep the text pattern in a local profile and use JSON only in deployed environments. Log viewers and command-line tools such as jq also make JSON readable when needed.
Should I log the full request and response body as fields?
Usually not. Bodies are large, often contain personal data, and make logs expensive. Log identifiers and outcomes as fields, and use tracing or a dedicated audit store for the rare cases that need payloads.
7Conclusion
In this answer, we saw that structured logs are worth the move because they are machine-parseable: no regular expressions, no broken multi-line events, and every detail is a field we can filter and group. Each event should carry timestamp, level, logger, message, service, version, trace id and span id, most of them supplied by configuration and tracing rather than by developers. Business identifiers belong in key-value fields with constant messages, and field names must be consistent across services, which adopting ECS and a small shared dictionary achieves.
Open with
I would move to structured logs because logs in production are read by machines first. With text logs, the log platform has to parse every line with regular expressions, which break silently when someone changes a pattern, and multi-line stack traces get split into many events.
Then, if they want more
Business details hidden in the message can only be found with noisy full-text searches. With structured JSON, every piece is a named field, so searching and grouping just work. For each event I would require the timestamp in UTC, level, logger, message, service name, version, environment, and the trace id and span id.
Service, version and environment come from configuration, which since Boot 3.4 is one property for the ECS format plus a few service properties, and Micrometer Tracing puts trace and span ids in the MDC, so developers do not type them.
The version lets me compare builds during a rollout, and the trace id lets me see one request across all services. For business data, I would use a constant message and key-value fields through the SLF4J fluent API, like order.id, rather than concatenating ids into the text.
And I would keep names consistent across services by adopting ECS and a small shared dictionary for our own fields.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why would you move from text logs to structured logs, and what fields would you require...": https://scrivio-interview-library.netlify.app/spring-boot/boot-logging/e74ef1f83c/