Last active
January 2, 2020 10:55
-
-
Save itsjavi/621b75e1c97946a389f30afb26033967 to your computer and use it in GitHub Desktop.
quick debug php function
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 | |
function quick_debug(string $message = '', array $params = []) | |
{ | |
$file = __DIR__ . '/debug-dev.log'; | |
$date = date('Y-m-d H:i:s.u'); | |
$params = array_map(function ($data) { | |
if (is_scalar($data)) { | |
return $data; | |
} | |
return var_export($data, true); | |
}, $params); | |
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3); | |
$caller = ''; | |
if (isset($trace[1])) { | |
// $trace[0] is this function | |
// $trace[1] is the caller | |
$caller = " @ {$trace[1]['class']} :: {$trace[1]['function']} : {$trace[1]['line']}"; | |
} | |
file_put_contents($file, $date . ($message ? ' ' . sprintf($message, ...$params) : '') . $caller . PHP_EOL, FILE_APPEND); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment