<?php
function exceptionHandler($exception) {
// these are our templates
$traceline = "#%s %s(%s): %s(%s)";
$msg = "PHP Fatal error: Uncaught exception '%s' with message '%s' in %s:%s\nStack trace:\n%s\n thrown in %s on line %s";
// alter your trace as you please, here
$trace = $exception->getTrace();
foreach ($trace as $key => $stackPoint) {
// I'm converting arguments to their type
// (prevents passwords from ever getting logged as anything other than 'string')
$trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);
}
// build your tracelines
$result = array();
foreach ($trace as $key => $stackPoint) {
$result[] = sprintf(
$traceline,
$key,
$stackPoint['file'],
$stackPoint['line'],
$stackPoint['function'],
implode(', ', $stackPoint['args'])
);
}
// trace always ends with {main}
$result[] = '#' . ++$key . ' {main}';
// write tracelines into main template
$msg = sprintf(
$msg,
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
implode("\n", $result),
$exception->getFile(),
$exception->getLine()
);
// log or echo as you please
error_log($msg);
}
set_exception_handler("exceptionHandler");
a();
function a()
{
throw new Exception("Just invoking the exception handler");
}
PHP Fatal error: Uncaught exception 'Exception' with message 'Just invoking the exception handler' in /in/DoYhS:54
Stack trace:
#0 /in/DoYhS(50): a()
#1 {main}
thrown in /in/DoYhS on line 54
Output for 5.0.3 - 5.0.4
Notice: Undefined index: args in /in/DoYhS on line 14
Warning: array_map(): Argument #2 should be an array in /in/DoYhS on line 14
Warning: implode(): Bad arguments. in /in/DoYhS on line 26
PHP Fatal error: Uncaught exception 'Exception' with message 'Just invoking the exception handler' in /in/DoYhS:54
Stack trace:
#0 /in/DoYhS(54): a()
#1 /in/DoYhS(50): a()
#2 {main}
thrown in /in/DoYhS on line 54
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_NEW in /in/DoYhS on line 54
Process exited with code 255.