Structured and plain-text logs
A plain-text log is a line written for a human, while a structured log records an event as field-value pairs, most often one-line JSON. A field is pulled from plain text with a regular expression, and any change of wording breaks the search, whereas stable field names and error codes avoid this. The stack trace goes into one field. Checks and alerts are tied to fields, not to a phrase.
Logs · Formats
How do you find every payment failure when each entry words the message differently?
A developer changed the message from "Payment failed" to "Could not process the payment", and a script that counted failures with grep silently shows zero. The test is green, there are "no failures". This is the typical result of plain-text logs: a machine searches for a substring in a phrase written for a human. This lesson covers the difference between plain-text and structured logs, why production moves to JSON, and what this gives automated tests.
How it works and why. A plain-text log is a free-form line. It is easy for the eye, but to extract a field from it you have to parse the phrase with a regular expression (grok in Logstash, regex in a collection agent), and any change of wording breaks the parser. A structured log writes the event as a set of "field: value" pairs, most often JSON on one line. Parsing comes down to standard JSON parsing, and a field has a stable name that changes less often than the message text.
2026-10-12 09:14:03,512 ERROR [checkout] Payment failed for order 8841: gateway timeout
2026-10-12 09:14:03,640 ERROR [checkout] Payment failed for order 8842: gateway timeout
{"ts":"2026-10-12T09:14:03.512Z","level":"error","service":"checkout",
"msg":"payment failed","orderId":8841,"error":"gateway_timeout",
"traceId":"4bf92f3577b34da6a3ce929d0e0e4736","durationMs":3004}
Another format often seen in Go and the Loki ecosystem is logfmt (level=error msg="payment failed" orderId=8841): "key=value" pairs separated by spaces. It is more readable than JSON but handles nested objects worse. The point is the same: a field is separated from the text.
The same query against a log
grep for a substring
Depends on wording, case and word order. Editing the message silently breaks the check.
Filter on a field
The error field and the number durationMs are stable. You can compare, group and count.
Example: the same search in two formats. The text variant:
# text: count payment failures for orders ending in 1
grep "Payment failed" app.log | grep -c "order [0-9]*1:"
# works only until someone changes the wording of the message
The expression is tied to the words "Payment failed", the position of the order number and the colon. The structured variant depends on none of these:
# JSON: a field and a value, the message wording does not matter
jq -c 'select(.error=="gateway_timeout" and .durationMs > 3000)' app.log
# count by error code
jq -s 'group_by(.error) | map({error: .[0].error, n: length})' app.log
The takeaway: a check "every payment failure in the last hour with a timeout over 3 s" is written on fields (error, durationMs), not on a phrase. In Kibana that is a KQL filter, in Loki | json | error="gateway_timeout", in a terminal jq.
Rules of a good structured log.
- One event is one line: the stack trace goes into a field (
"stack") as a string instead of being spread over dozens of lines. - Common field names across all services (
traceId,userId), otherwise search across the system falls apart. A ready schema is often used, such as Elastic Common Schema. - A stable error code (
"error":"gateway_timeout") next to the text: the code for the machine, the text for the human. - Do not log large request and response bodies whole: volume and leak risk.
Limits and alternatives. A structured format does not fix bad logs: a field "msg":"error" is useless in any format. JSON is hard to read by eye, so locally developers turn on a "pretty" output and keep JSON in production. Multi-line exceptions in a text format can be torn by an aggregator into separate entries:
2026-10-12 09:14:03,512 ERROR [checkout] Payment failed
java.net.SocketTimeoutException: Read timed out
at ...
2026-10-12 09:14:03,640 INFO [checkout] Retry scheduled
Here a line with at ... and the next INFO, unless a multi-line rule is configured, are seen by the aggregator as separate events. For QA this means: when looking for a root cause, check that the whole stack collected into a single event.
Typical mistakes.
- An automated test looks for an exact message phrase in a log: it breaks on every wording change.
- Different names of one field in different services (
user_id,userId,uid): one query does not find everything. - A stack split into several entries: the "first error" in a search looks empty.
- Values of different types in one field (
orderIdis a number, then a string): the storage index rejects some entries.
Check yourself
A test looks for the string "Payment failed" in a log and turned green after a refactoring, although failures exist. What happened and how do you make the check robust?
Answer
The message was reworded and the substring no longer matches, so the search finds nothing and a "no failures" check passes. Search on a stable field (error code, event type) in a structured log, and also add a check that the result is not empty where at least one entry is expected.
Why is a stack trace put into one field of a JSON log instead of being left as many lines?
Answer
An aggregator reads a log line by line and treats each line as a separate event. A multi-line stack falls apart and the link to the error is lost. In one field all frames stay part of one event, and a search by request id finds the full stack.