A PHP class for dumping data to a separate log file

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.

One last thing…

If you liked this article, I'd be incredibly grateful if you tweeted about it.

Also, I don't send emails often but when I do, I try to fill them full of useful goodies. If you like code snippets & dev tips, join my mailing list. There's no catch but I'll probably want to tell you about any new plugins & tools I build.