Though this is an old question, I've been looking for this. Here's my compilation of some solutions answered here and some other ideas found elsewhere to get a one-size-fits-all solution.
CODE :
// Post to browser console
function console[$data, $is_error = false, $file = false, $ln = false] {
if[!function_exists['console_wer']] {
function console_wer[$data, $is_error = false, $bctr, $file, $ln] {
echo ''.''.[[$is_error!==false] ? 'if[typeof phperr_to_cns === \'undefined\'] { var phperr_to_cns = 1; document.addEventListener["DOMContentLoaded", function[] { setTimeout[function[]{ alert["Alert. see console."]; }, 4000]; }]; }' : ''].' console.group["PHP '.[[$is_error] ? 'error' : 'log'].' from "+window.atob["'.base64_encode[[[$file===false] ? $bctr['file'] : $file]].'"]'.[[[$ln!==false && $file!==false] || $bctr!==false] ? '+" on line '.[[$ln===false] ? $bctr['line'] : $ln].' :"' : '+" :"'].']; console.'.[[$is_error] ? 'error' : 'log'].'['.[[is_array[$data]] ? 'JSON.parse[window.atob["'.base64_encode[json_encode[$data]].'"]]' : '"'.$data.'"'].']; console.groupEnd[];'; return true;
}
}
return @console_wer[$data, $is_error, [[$file===false && $ln===false] ? array_shift[debug_backtrace[]] : false], $file, $ln];
}
//PHP Exceptions handler
function exceptions_to_console[$svr, $str, $file, $ln] {
if[!function_exists['severity_tag']] {
function severity_tag[$svr] {
$names = [];
$consts = array_flip[array_slice[get_defined_constants[true]['Core'], 0, 15, true]];
foreach [$consts as $code => $name] {
if [$svr & $code] $names []= $name;
}
return join[' | ', $names];
}
}
if [error_reporting[] == 0] {
return false;
}
if[error_reporting[] & $svr] {
console[severity_tag[$svr].' : '.$str, true, $file, $ln];
}
}
// Divert php error traffic
error_reporting[E_ALL];
ini_set["display_errors", "1"];
set_error_handler['exceptions_to_console'];
TESTS & USAGE :
Usage is simple. Include first function for posting to console manually. Use second function for diverting php exception handling. Following test should give an idea.
// Test 1 - Auto - Handle php error and report error with severity info
$a[1] = 'jfksjfks';
try {
$b = $a[0];
} catch [Exception $e] {
echo "jsdlkjflsjfkjl";
}
// Test 2 - Manual - Without explicitly providing file name and line no.
console[array[1 => "Hi", array["hellow"]], false];
// Test 3 - Manual - Explicitly providing file name and line no.
console[array[1 => "Error", array[$some_result]], true, 'my file', 2];
// Test 4 - Manual - Explicitly providing file name only.
console[array[1 => "Error", array[$some_result]], true, 'my file'];
EXPLANATION :
The function
console[$data, $is_error, $file, $fn]
takes string or array as first argument and posts it on console using js inserts.Second argument is a flag to differentiate normal logs against errors. For errors, we're adding event listeners to inform us through alerts if any errors were thrown, also highlighting in console. This flag is defaulted to false.
Third and fourth arguments are explicit declarations of file and line numbers, which is optional. If absent, they're defaulted to using the predefined php function
debug_backtrace[]
to fetch them for us.Next function
exceptions_to_console[$svr, $str, $file, $ln]
has four arguments in the order called by php default exception handler. Here, the first argument is severity, which we further crosscheck with predefined constants using functionseverity_tag[$code]
to provide more info on error.
NOTICE :
Above code uses JS functions and methods that are not available in older browsers. For compatibility with older versions, it needs replacements.
Above code is for testing environments, where you alone have access to the site. Do not use this in live [production] websites.
SUGGESTIONS :
First function
console[]
threw some notices, so I've wrapped them within another function and called it using error control operator '@'. This can be avoided if you didn't mind the notices.Last but not least, alerts popping up can be annoying while coding. For this I'm using this beep [found in solution : //stackoverflow.com/a/23395136/6060602] instead of popup alerts. It's pretty cool and possibilities are endless, you can play your favorite tunes and make coding less stressful.