Debug by diffing logs
2025-06-29
The Problem
I have 2 versions of an algorithm that calculates a number based on a large input file (thousands of lines). I know v2 is the correct one, but I want to know what inputs v1 is handling incorrectly.
The Solution
I added structured debug logs printing the inputs of each call to my main function. Then, I piped the logs for both versions to text files and I ran a diff on them to see the differences:
go run main.go > v2.txt
# use git to go back in time
go run main.go > v1.txt
diff ./v1.txt ./v2.txt
1153c1153
< report= [63 66 64 63 62 61 60 57] isSafe= false
---
> report= [63 66 64 63 62 61 60 57] isSafe= true
1361c1361
< report= [94 92 94 95 97 99] isSafe= false
---
> report= [94 92 94 95 97 99] isSafe= true
2005c2005
< total safe reports: 559
---
> total safe reports: 561
Noice! Now I can add those 2 cases to my unit tests and have better coverage of edge cases.
Final thoughts
I’ve used this approach in the past with logs from a server, but it never occurred to me to do it with logs on my local machine. I was trying to think why this idea occurred to me now and the only reason I can think of is Vim. I predicted in this post that using Vim would force me to use the terminal and here we are!
Also, look at how nice the syntax highlighting looks in the diff codeblock. I learned just now that diff
is a supported language. I’m gonna start using this all the time for posts and documentation 😍