Debugging can be a lot simpler with a nice set of tools. Ideally, breakpoints and stepping through code is the way to go but sometimes it is nice and easy just to dump stuff somewhere.
Dumping to the DOM is usually fine but can obviously cause problems on the page render and it can be tricky if/when you want to dump multiple things. So, I tend to opt for dumping to a log file.
I usually also monitor the file in my command line using tail -f dev.log
so I can see when things are appended to the file.
Here is a static PHP class I use to keep this easy. I tend to include and configure it in my wp-config.php
file but you can pull this in anywhere you need it. Keep in mind, however, that the static nature of this class means you can’t create multiple logs from it. For that, you’d need to tweak it a little and use instances but I only use one dev log so this makes perfect sense to me the way it is.