-
-
Save ihorvorotnov/6c5664e6003baf32bd68871c0c0c007e to your computer and use it in GitHub Desktop.
Simple debug trace to wp-content/debug.log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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