diff options
author | Kyle Kaminski <kyle@xenomedia.com> | 2014-04-01 01:22:07 -0500 |
---|---|---|
committer | Kyle Kaminski <kyle@xenomedia.com> | 2014-04-01 01:22:07 -0500 |
commit | 73a386618382e3f701c83fa46f6846ce0e250c86 (patch) | |
tree | 8776f4d78e7408cf7f79fc1ec7d3b3ac5a39f1e8 | |
parent | 1bad4fc00814e2c03ecadaa7faf93c6372f5bd30 (diff) | |
download | phpsandbox-73a386618382e3f701c83fa46f6846ce0e250c86.tar.gz phpsandbox-73a386618382e3f701c83fa46f6846ce0e250c86.tar.bz2 phpsandbox-73a386618382e3f701c83fa46f6846ce0e250c86.zip |
learn how to print backtraces in php
-rw-r--r-- | error_log-to-currdir.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/error_log-to-currdir.php b/error_log-to-currdir.php new file mode 100644 index 0000000..32d1de4 --- /dev/null +++ b/error_log-to-currdir.php @@ -0,0 +1,61 @@ +<?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; +} |