Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Last active January 2, 2020 10:55
Show Gist options
  • Save itsjavi/621b75e1c97946a389f30afb26033967 to your computer and use it in GitHub Desktop.
Save itsjavi/621b75e1c97946a389f30afb26033967 to your computer and use it in GitHub Desktop.
quick debug php function
<?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