Skip to content

Instantly share code, notes, and snippets.

@ihorvorotnov
Forked from evanre/log.php
Last active June 29, 2017 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihorvorotnov/6c5664e6003baf32bd68871c0c0c007e to your computer and use it in GitHub Desktop.
Save ihorvorotnov/6c5664e6003baf32bd68871c0c0c007e to your computer and use it in GitHub Desktop.
Simple debug trace to wp-content/debug.log
<?php
/**
* Simple debug trace to wp-content/debug.log
*
* @usage _log( $var );
*
* @param $log mixed A string|array|object you want to dump into wp-content/debug.log
*/
if ( ! function_exists( '_log' ) ) {
// Use built-in logging or write to file directly, depending on the WP_DEBUG constant value
function _log( $log ) {
// If debug mode is ON, just dump into the log file
if ( true == WP_DEBUG ) {
_print_log( $log );
// If debug mode is OFF (usually, on production), write into file instead
} else {
ob_start();
$timestamp = date('d-M-Y h:i:s T');
echo "[{$timestamp}]\r\n";
_print_log( $log );
echo "\r\n";
file_put_contents( ABSPATH . 'wp-content/debug.log', ob_get_contents(), FILE_APPEND );
ob_end_clean();
}
}
// Outout the contents of $log var depending on its type
function _print_log( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
print_r( $log );
} else {
echo( $log );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment