如何获取PHP的getTraceAsString()的完整字符串?

时间:2009-12-22 21:36:09

标签: php string exception truncation truncated

我正在使用getTraceAsString()来获取堆栈跟踪但由于某种原因字符串被截断。

示例,抛出异常并使用以下命令记录字符串:

catch (SoapFault $e) {
error_log( $e->getTraceAsString() )
}

打印出的字符串是:

  

#0 C:\ Somedirectory \ Somedirectory \ Somedirectory \ Somedir \ SomeScript.php(10):SoapClient-> SoapClient('http://www.ex ...')

如何获取要打印的完整字符串?

8 个答案:

答案 0 :(得分:29)

我创建了这个函数来返回没有截断字符串的堆栈跟踪:

function getExceptionTraceAsString($exception) {
    $rtn = "";
    $count = 0;
    foreach ($exception->getTrace() as $frame) {
        $args = "";
        if (isset($frame['args'])) {
            $args = array();
            foreach ($frame['args'] as $arg) {
                if (is_string($arg)) {
                    $args[] = "'" . $arg . "'";
                } elseif (is_array($arg)) {
                    $args[] = "Array";
                } elseif (is_null($arg)) {
                    $args[] = 'NULL';
                } elseif (is_bool($arg)) {
                    $args[] = ($arg) ? "true" : "false";
                } elseif (is_object($arg)) {
                    $args[] = get_class($arg);
                } elseif (is_resource($arg)) {
                    $args[] = get_resource_type($arg);
                } else {
                    $args[] = $arg;
                }   
            }   
            $args = join(", ", $args);
        }
        $rtn .= sprintf( "#%s %s(%s): %s(%s)\n",
                                 $count,
                                 $frame['file'],
                                 $frame['line'],
                                 $frame['function'],
                                 $args );
        $count++;
    }
    return $rtn;
}

或者,您可以编辑截断输出的php源:https://github.com/php/php-src/blob/master/Zend/zend_exceptions.c#L392

答案 1 :(得分:5)

一些更好的版本https://stackoverflow.com/a/6076667/194508在这里https://gist.github.com/1437966添加了要输出的类。

答案 2 :(得分:4)

这个解决方案很好但是在我的情况下它会抛出一个错误,因为我的跟踪中有内部函数。我添加了几行代码来检查,以便跟踪功能仍然有效。

function getExceptionTraceAsString($exception) {
    $rtn = "";
    $count = 0;
    foreach ($exception->getTrace() as $frame) {


        $args = "";
        if (isset($frame['args'])) {
            $args = array();
            foreach ($frame['args'] as $arg) {
                if (is_string($arg)) {
                    $args[] = "'" . $arg . "'";
                } elseif (is_array($arg)) {
                    $args[] = "Array";
                } elseif (is_null($arg)) {
                    $args[] = 'NULL';
                } elseif (is_bool($arg)) {
                    $args[] = ($arg) ? "true" : "false";
                } elseif (is_object($arg)) {
                    $args[] = get_class($arg);
                } elseif (is_resource($arg)) {
                    $args[] = get_resource_type($arg);
                } else {
                    $args[] = $arg;
                }
            }
            $args = join(", ", $args);
        }
        $current_file = "[internal function]";
        if(isset($frame['file']))
        {
            $current_file = $frame['file'];
        }
        $current_line = "";
        if(isset($frame['line']))
        {
            $current_line = $frame['line'];
        }
        $rtn .= sprintf( "#%s %s(%s): %s(%s)\n",
            $count,
            $current_file,
            $current_line,
            $frame['function'],
            $args );
        $count++;
    }
    return $rtn;
}

答案 3 :(得分:2)

更改php.ini设置log_errors_max_len会有帮助吗?

此外,请注意,邮件仅在输出期间被截断,您仍然可以通过调用$ exception-> getMessage()

来获取原始错误消息

答案 4 :(得分:1)

Ernest Vogelsinger在https://www.php.net/manual/exception.gettraceasstring.php#114980也提供了优秀的Permission denied配方,它支持链式异常,并以类似Java的方式进行格式化。

以下是他对php.net的评论直接比较:

Exception :: getTraceAsString:

jTraceEx

jTraceEx:

#0 /var/htdocs/websites/sbdevel/public/index.php(70): seabird\test\C->exc()
#1 /var/htdocs/websites/sbdevel/public/index.php(85): seabird\test\C->doexc()
#2 /var/htdocs/websites/sbdevel/public/index.php(89): seabird\test\fail2()
#3 /var/htdocs/websites/sbdevel/public/index.php(93): seabird\test\fail1()
#4 {main}

答案 5 :(得分:0)

您可以使用

打印回溯
MiddleName   LastName
john            .
james        phillips
Mary            .

The procedure should then make it:
MiddleName   LastName
             john 
james        phillips
             Mary                                                                                     

它不会截断。

示例打印为

debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

答案 6 :(得分:0)

在抛出异常的情况下,可以提供先前的异常作为第三个参数。这样,可以链接“异常”跟踪。

try  {
    f('123');
} catch(Throwable $e){
    var_dump($e);
}

function f($arg){
    if(is_string($arg)){
        try {
            g($arg);
        } catch(UnexpectedValueException $e) {
            // Supply a third argument to pass the previous Exception.
            throw new RuntimeException('Error in function g()', $e->getCode(), $e);
        } catch(Throwable $e) {
            // Supply a third argument to pass the previous Exception.
            throw new RuntimeException('Unkown Error in function g()', $e->getCode(), $e);
        }   
    }   
}

function g($string){
    if(strlen($string) < 6){
        try {
            h($string);
        } catch(UnexpectedValueException $e) {
            throw new UnexpectedValueException('String is smaller then 6', $e->getCode(), $e);
        }
    }
    return $string;
}

function h($string){
    if(strlen($string) < 4){
        throw new UnexpectedValueException('String is smaller then 4');
    }
    return $string;
}

输出:

C:\wamp64\www\julian\index.php:21:
object(RuntimeException)[3]
  protected 'message' => string 'Error in function g()' (length=21)
  private 'string' (Exception) => string '' (length=0)
  protected 'code' => int 0
  protected 'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
  protected 'line' => int 30
  private 'trace' (Exception) => 
    array (size=1)
      0 => 
        array (size=4)
          'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
          'line' => int 19
          'function' => string 'f' (length=1)
          'args' => 
            array (size=1)
              0 => string '123' (length=3)
  private 'previous' (Exception) => 
    object(UnexpectedValueException)[2]
      protected 'message' => string 'String is smaller then 6' (length=24)
      private 'string' (Exception) => string '' (length=0)
      protected 'code' => int 0
      protected 'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
      protected 'line' => int 43
      private 'trace' (Exception) => 
        array (size=2)
          0 => 
            array (size=4)
              'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
              'line' => int 27
              'function' => string 'g' (length=1)
              'args' => 
                array (size=1)
                  0 => string '123' (length=3)
          1 => 
            array (size=4)
              'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
              'line' => int 19
              'function' => string 'f' (length=1)
              'args' => 
                array (size=1)
                  0 => string '123' (length=3)
      private 'previous' (Exception) => 
        object(UnexpectedValueException)[1]
          protected 'message' => string 'String is smaller then 4' (length=24)
          private 'string' (Exception) => string '' (length=0)
          protected 'code' => int 0
          protected 'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
          protected 'line' => int 51
          private 'trace' (Exception) => 
            array (size=3)
              0 => 
                array (size=4)
                  'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
                  'line' => int 41
                  'function' => string 'h' (length=1)
                  'args' => 
                    array (size=1)
                      0 => string '123' (length=3)
              1 => 
                array (size=4)
                  'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
                  'line' => int 27
                  'function' => string 'g' (length=1)
                  'args' => 
                    array (size=1)
                      0 => string '123' (length=3)
              2 => 
                array (size=4)
                  'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
                  'line' => int 19
                  'function' => string 'f' (length=1)
                  'args' => 
                    array (size=1)
                      0 => string '123' (length=3)
          private 'previous' (Exception) => null
          public 'xdebug_message' => string '<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> UnexpectedValueException: String is smaller then 4 in C:\wamp64\www\julian\index.php on line <i>51</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361152</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\julian\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361528</td><td bgcolor='#eeeeec'>f(  )</td><td title='C'... (length=1645)
      public 'xdebug_message' => string '<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> UnexpectedValueException: String is smaller then 6 in C:\wamp64\www\julian\index.php on line <i>43</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361152</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\julian\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361528</td><td bgcolor='#eeeeec'>f(  )</td><td title='C'... (length=1376)
  public 'xdebug_message' => string '<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> RuntimeException: Error in function g() in C:\wamp64\www\julian\index.php on line <i>30</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361152</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\julian\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361528</td><td bgcolor='#eeeeec'>f(  )</td><td title='C:\wamp64\ww'... (length=1096)

答案 7 :(得分:-3)

如果你可以使用var_dump(),那么一个简单的解决方案就是:

try {
   ...
} catch (Exception $e)
   var_dump($e->getTrace());
}

从这个伟大的answer by Andre

中偷走了