How to compare two log files and find where a run went wrong
By TechCompare · Updated · How we check
Remove timestamps and other values that change on every run, then diff the two logs. The first changed line is usually the closest one to the cause.
The example
1 change: 2 lines removed, 3 added
| Original: worker.log | Changed: worker.log | ||||
|---|---|---|---|---|---|
| 1 | INFO Starting worker pool size=4 | 1 | INFO Starting worker pool size=4 | ||
| 2 | INFO Connected to postgres at db:5432 | 2 | INFO Connected to postgres at db:5432 | ||
| 3 | INFO Loaded 1200 jobs from queue | 3 | INFO Loaded 1200 jobs from queue | ||
| 4 | Removed | WARN Job 381 retried after timeout | 4 | Added | ERROR Job 381 failed: connection reset |
| 5 | Removed | INFO Finished | 5 | Added | ERROR Job 977 failed: connection reset |
| 6 | Added | INFO Finished 1198 jobs in 131 s | |||
When a job works on Monday and fails on Tuesday, the logs of both runs hold the answer. Reading them side by side is slow, because most lines match. A diff skips the lines that are the same and takes you straight to the first place where the two runs did something different.
How this is calculated
The example compares the end of two worker logs, with the timestamps already removed. The first three lines match, so they show as context. Then the runs split: the first one retried job 381 after a timeout, while the second failed it outright and failed job 977 too. The tool reports 1 change: 2 lines removed, 3 added. The retry and error lines are mostly different, so they show without word marks. In the last line, the marks land on the job count and the run time.
Real logs need some cleanup before a diff is useful. Almost every log line starts with a timestamp, so two runs never match line for line. Strip it first. If each line starts with an ISO 8601 time and a space, this removes it:
sed -E 's/^[0-9T:.+-]+Z? //' run-1.log > run-1.clean.logOther values change from run to run too: process ids, request ids, durations and temporary file names. Replace each of them with a fixed word, like sed -E 's/request_id=[a-z0-9-]+/request_id=X/', so they stop showing up as changes.
The tool takes up to 5 MB per side, which is tens of thousands of typical log lines. Big texts are compared in a background worker, so the page stays responsive while it runs. For anything past that, cut the logs down to the time window that matters with grep or sed first.
Verdict
Diff the logs of two runs of the same job on the same input, after you strip timestamps and ids. Start reading at the first change, not the last. A failure at the end of a log is often the result of something that went wrong earlier, and the first difference is the best place to look.
More Text Diff scenarios
Related guides
Frequently asked questions
Why does every line of my log show as changed?
Can I compare log files bigger than 5 MB?
Are my logs uploaded?
Related tools
JSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use toolCharacter Counter
Count characters, words, sentences, paragraphs, and lines. Check writing limits and count specific characters or phrases.
Use toolText Encoding Converter
Convert text to Base64, binary, hex, URL encoding and HTML entities, and back again.
Use tool