Prompt
How do I parse multiline stack traces into searchable logs?
Latest observation
To make multiline stack traces searchable, the key is to keep each exception as a single log event instead of splitting it into separate lines.
Common approaches
1) Emit structured logs if possible
Best option: log exceptions as JSON with fields like:
timestamplevelmessageexception.typeexception.messageexception.stack_trace
This makes search and filtering much easier than raw text.
2) Use multiline parsing in your log collector
If your app writes plain text stack traces, configure your log shipper/parser to join related lines into one event.
Typical multiline rule:
- Treat a line starting with a timestamp / log prefix as the start of a new event
- Any line that does not match that pattern gets appended to the previous line
Example pattern idea
If your logs start like:
2026-10-22 12:00:01 INFO Starting app
2026-10-22 12:00:05 ERROR Something failed
java.lang.NullPointerException: boom
at com.example.Foo.bar(Foo.java:12)
at com.example.App.main(App.java:8)
2026-10-22 12:00:06 INFO Recovering
Use the timestamp line as the delimiter so the exception block becomes one record.
3) Preserve stack traces as escaped newlines
If you’re sending logs directly to a searchable system:
- replace actual newlines with
\n - store the entire stack trace in one field
Example:
{
"level": "ERROR",
"message": "Something failed",
"stack_trace": "java.lang.NullPointerException: boom\n at com.example.Foo.bar(Foo.java:12)\n at com.example.App.main(App.java:8)"
}
How to do it in popular tools
Fluent Bit / Fluentd
Use a multiline parser that groups lines based on a regex match for the first line of a log event.
Logstash
Use the multiline codec or preprocess in Filebeat/Fluent Bit before sending.
Filebeat
Use the multiline configuration to join stack trace lines into one event.
Docker / Kubernetes
If logs are split by container runtime, configure your collector to merge multiline output before indexing.
Good multiline parsing rules
A reliable “start of event” line often includes:
- timestamp
- log level
- thread name
- logger name
Example regex:
^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}
Then:
- new event if line matches regex
- append otherwise
Important cautions
- Don’t rely only on indentation; stack traces vary by language and framework
- Make sure your parser handles:
Caused by:Suppressed:- nested exceptions
- Python tracebacks
- JavaScript async stack traces
- Avoid splitting on blank lines alone, since many logs contain blank lines normally
Recommended best practice
- Use structured JSON logs
- If not possible, configure multiline parsing at the ingestion layer
- Store stack traces in a dedicated field for search and filtering
If you want, I can give you a ready-to-use multiline config for:
- Elasticsearch/Filebeat
- Logstash
- Fluent Bit
- Python
- Java / Logback
- Kubernetes logs