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.

* Class Dump
* A simple static class for dumping data to a separate log file to aid debugging and development.
* @author Phil Kurth <[email protected]>
class Dump {
public static $logfile;
* Logs data to log file
* @param mixed $data
* @param string $mode Specifies write mode ('a' for append || 'w' for write)
* @param boolean $pretty If true, var_export is used
* @param boolean $informative If true, informative header is printed
* @throws \Exception
static function log( $data, $informative = false, $mode = 'a', $pretty = true ) {
if ( ! self::$logfile ) {
throw new \Exception( 'Debug::$logfile is not set' );
$file_location = self::$logfile;
$datetime = new DateTime; // current time = server time
$otherTZ = new DateTimeZone( 'Australia/Melbourne' );
$datetime->setTimezone( $otherTZ ); // calculates with new TZ now
$bt = debug_backtrace();
$file = "Calling file: " . basename( $bt[0]['file'] );
$line = "Line: " . $bt[0]['line'];
$info = '';
if ( $informative ) {
$info = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
$info .= $datetime->format( 'm/d/Y h:i:s a' ) . "\n";
$info .= $file . "\n";
$info .= $line . "\n";
$info .= ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
// Better boolean logging
if ( $data === false ) {
$data = '(bool)FALSE';
} elseif ( $data === true ) {
$data = '(bool)TRUE';
if ( $pretty ) {
$info .= print_r( $data, true );
} else {
$info .= var_export( $data, true );
$info .= $informative ? "\n\n" : "\n";
$file = fopen( $file_location,
$mode ) or print( '<div style="background-color:#db514d;color:white;text-align:center;padding:10px;">Cannot open log file for logging</div>' );
fwrite( $file, $info );
fclose( $file );
view raw Dump.php hosted with ❤ by GitHub
// Include and configure the class. I usually do this in my wp-config.php
// file but you could do this in a theme or a plugin instead.
require 'class-dump.php';
Dump::$logfile = __DIR__ . '/dev.log';
// Need to dump something to the log while you are working? Easy!
Dump::log('some data');

Need a website? Let's talk.

From website design & SEO through to custom WordPress plugin development. I transform ideas into dynamic, engaging, and high-performing solutions.
Subscribe to get the latest insights & updates in the world of web and how it impacts your business and website.
© 2024 Phil Kurth  |  All rights reserved.