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.logChanged: worker.log
1INFO Starting worker pool size=41INFO Starting worker pool size=4
2INFO Connected to postgres at db:54322INFO Connected to postgres at db:5432
3INFO Loaded 1200 jobs from queue3INFO Loaded 1200 jobs from queue
4RemovedWARN Job 381 retried after timeout4AddedERROR Job 381 failed: connection reset
5RemovedINFO Finished 1200 jobs in 94 s5AddedERROR Job 977 failed: connection reset
6AddedINFO 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.

Changes in the example
1
2 lines removed, 3 added
Lines
5 and 6
Original and changed text
Category
Files and data
Text diff guides

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:

Shell
sed -E 's/^[0-9T:.+-]+Z? //' run-1.log > run-1.clean.log

Other 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

Frequently asked questions

Why does every line of my log show as changed?
Each line probably starts with a timestamp, and the timestamps never match between runs. Remove them before comparing, for example with sed, and only the real differences remain.
Can I compare log files bigger than 5 MB?
Not in this tool. Each side holds up to 5 MB. Cut a large log down to the part you need first, like the minutes around the failure, using grep, sed, head or tail.
Are my logs uploaded?
No. The comparison runs in your browser, and the log text stays out of the page URL. That matters for logs, which often hold user ids, IP addresses or tokens.