Better debug logging
2025-01-02 06:45:00 +07:00 by Mark Smith
I’m on a bit of a mission to improve the debug logging of my static site generator. I currently use the debug module which is much used in nodejs development because it’s very simple. You can easily turn on and off logging on a per file basis by specifying them in the DEBUG environment variable. It’s nice and simple, pretty much does what it says on the tin.
The problem though is that debug logging and logging from console.log are not synchronised. For command line programs like my static site generator that’s an issue because all the output gets interleaved all out of sync so it’s very difficult, often impossible to follow the flow of the program. It ends up often looking like stuff from the future is happening in the past, and vice versa.
It’s all because console.log outputs to stdout while debug goes to stderr. I think it’s something to do with the fact that stdout and stderr buffer differently. So I’m looking at ways to configure debug to output everything to stdout. That way all the output should stay in sync. I’ll likely try to make it configurable because it is sometimes handy to have a way to seperate regular logging output from debug logging.
I’m also looking at refactoring the plugins, extracting out utility functions to seperate files. That makes it easier to be more granular with what gets output to the screen in a debug session. When everything is in one file, you get overwhelmed with output. I’ve updated the blog plugin already, so I’ll likely do the others next.
Honestly this probably wouldn’t even be necessary if I had a laptop. It really becomes an issue when you are debugging stuff in github actions and don’t have the luxury of quickly jumping between terminal windows. #