How to print PHP call stack to generate a debug log

2 篇文章 0 订阅
1 篇文章 0 订阅

欢迎转载,请注明作者及出处


1st Way:Use PHP methoddebug_backtrace and/ordebug_print_backtrace to generate a backtrace.

though those 2 methods you will get an array like this one :

array(2) {
[0]=>
array(4) {
    ["file"] => string(10) "/tmp/a.php"
    ["line"] => int(10)
    ["function"] => string(6) "a_test"
    ["args"]=>
    array(1) {
      [0] => &string(6) "friend"
    }
}
[1]=>
array(4) {
    ["file"] => string(10) "/tmp/b.php"
    ["line"] => int(2)
    ["args"] =>
    array(1) {
      [0] => string(10) "/tmp/a.php"
    }
    ["function"] => string(12) "include_once"
  }
}

Note:They will apparently not flush the I/O buffer, but you can do that yourself, withflush and/orob_flush.


2nd Way: Exception::getTraceAsString()

When got an exception we can use instance of Exception to instead ofdebug_backtrace . It's morereadable.

For example:

$e = new Exception;
var_dump($e->getTraceAsString());

#2 /usr/share/php/PHPUnit/Framework/TestCase.php(626): SeriesHelperTest->setUp()
#3 /usr/share/php/PHPUnit/Framework/TestResult.php(666): PHPUnit_Framework_TestCase->runBare()
#4 /usr/share/php/PHPUnit/Framework/TestCase.php(576): PHPUnit_Framework_TestResult->run(Object(SeriesHelperTest))
#5 /usr/share/php/PHPUnit/Framework/TestSuite.php(757): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#6 /usr/share/php/PHPUnit/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->runTest(Object(SeriesHelperTest), Object(PHPUnit_Framework_TestResult))
#7 /usr/share/php/PHPUnit/TextUI/TestRunner.php(305): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#8 /usr/share/php/PHPUnit/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#9 /usr/share/php/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#10 /usr/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#11 {main}"

If you need to do more complex formatting,use function defined below to format output dump_value or you can get a format string by requirement:

/**
 * format output dump_value or you can get a format string by requirement
 * @param type $var
 * @param type $echo
 * @param type $label
 * @param type $strict
 * @return string 
 */
function formatDump($var, $echo=true,$label=null, $strict=true)
{
    
    $label = ($label===null) ? '' : rtrim($label) . ' ';
    //If needn't strict
    if(!$strict) {
        if (ini_get('html_errors')) {
            $output = print_r($var, true);
            $output = "<pre>".$label.htmlspecialchars($output,ENT_QUOTES)."</pre>";
        } else {
            $output = $label . " : " . print_r($var, true);
        }
    }else {
        //Turn on output buffering
        ob_start();
        //output it to buffer
        var_dump($var);
        //Get current buffer contents and delete current output buffer
        $output = ob_get_clean();
        //Find out whether 'xdebug' extension is loaded
        if(!extension_loaded('xdebug')) {
            //'m' means enhanced line anchor
            $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
            $output = '<pre>'. $label. htmlspecialchars($output, ENT_QUOTES). '</pre>';
        }
    }
    //if output on response
    if ($echo) {
        echo($output);
        return null;
    }else
        return $output;
}//end if






reference link:http://stackoverflow.com/questions/1423157/print-php-call-stack


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值