<?php error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 1); ini_set('log_errors', 1); ini_set('log_errors_max_len', 0); /* unlimited */ ini_set('error_log', './php_errors.log'); register_shutdown_function(function() { // error_log func not available here due to fatal php shutdown //error_log(print_r(debug_backtrace(), TRUE)); }); function a($arg) { b('beta'); } function b($arg) { c('charlie'); } function c($arg) { d('delta'); } function d($arg) { /* calling error_log in register_shutdown_function is too late, however var_dump still works there, in case of error * in real code insert the line below above the line causing trouble */ error_log(print_r(debug_backtrace(), TRUE)); /* nicer output */ $e = new Exception; error_log($e->getTraceAsString()); /* print to screen in simple format */ print mydebug_bt(); trigger_error("user triggered error", E_USER_ERROR); } error_log('calling a()', 0, './php_errors.log'); a('alpha'); /* simplify and beatify the output of a dump of debug_backtrace() */ function mydebug_bt() { $stack = ''; $i = 1; $trace = debug_backtrace(); unset($trace[0]); // remove call to this function from stack trace foreach($trace as $node) { $stack .= "#$i ".$node['file'] ."(" .$node['line']."): "; if(isset($node['class'])) { $stack .= $node['class'] . "->"; } $stack .= $node['function'] . "()" . PHP_EOL; $i++; } return $stack; }