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

发布于 2024-08-15 13:45:24 字数 421 浏览 2 评论 0原文

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

例如,抛出异常,我使用以下方式记录字符串:

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

打印出来的字符串是:

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

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

I'm using getTraceAsString() to get a stack trace but the string is being truncated for some reason.

Example, an exception is thrown and I log the string using:

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

The string thats prints out is:

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

How can I get the full string to print?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

请别遗忘我 2024-08-22 13:45:24

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

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%s(%s)\n",
            $count,
            $frame['file'],
            $frame['line'],
            isset($frame['class']) ? $frame['class'] : '',
            isset($frame['type']) ? $frame['type'] : '', // "->" or "::"
            $frame['function'],
            $args
        );
        $count++;
    }
    return $rtn;
}

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

I created this function to return a stack trace with no truncated strings:

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%s(%s)\n",
            $count,
            $frame['file'],
            $frame['line'],
            isset($frame['class']) ? $frame['class'] : '',
            isset($frame['type']) ? $frame['type'] : '', // "->" or "::"
            $frame['function'],
            $args
        );
        $count++;
    }
    return $rtn;
}

Alternatively, you could edit the php source where it is truncating the output: https://github.com/php/php-src/blob/master/Zend/zend_exceptions.c#L392

音栖息无 2024-08-22 13:45:24

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

Some better version https://stackoverflow.com/a/6076667/194508 is here https://gist.github.com/1437966 added class to output.

皇甫轩 2024-08-22 13:45:24

该解决方案很好,但就我而言,它引发了错误,因为我的跟踪中有内部函数。我添加了几行代码来检查这一点,以便跟踪功能仍然有效。

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;
}

That solution is good but in my case it threw an error because my trace has internal functions in it. I added a few lines of code to check for that so the trace functions still works.

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;
}
可是我不能没有你 2024-08-22 13:45:24

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

另外,请注意,消息仅在输出期间被截断,您仍然可以通过调用 $exception->getMessage() 来获取原始错误消息

Will changing the php.ini setting log_errors_max_len help?

Also, please note that messages are truncated only during the output, you still can get the original error message with call to $exception->getMessage()

走野 2024-08-22 13:45:24

还有 Ernest Vogelsinger 的优秀 jTraceEx 配方,位于 https://www.php.net/manual/exception.gettraceasstring.php#114980,支持链式异常,并以类似 Java 的方式格式化。

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

Exception::getTraceAsString :

#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}

jTraceEx :

Exception: Thrown from class C
 at seabird.test.C.exc(index.php:78)
 at seabird.test.C.doexc(index.php:70)
 at seabird.test.fail2(index.php:85)
 at seabird.test.fail1(index.php:89)
 at (main)(index.php:93)
Caused by: Exception: Thrown from class B
 at seabird.test.B.exc(index.php:64)
 at seabird.test.C.exc(index.php:75)
 ... 4 more
Caused by: Exception: Thrown from class A
 at seabird.test.A.exc(index.php:46)
 at seabird.test.B.exc(index.php:61)
 ... 5 more

There is also the excellent jTraceEx recipe by Ernest Vogelsinger at https://www.php.net/manual/exception.gettraceasstring.php#114980, that supports chained exceptions and is formatted in a Java-like manner.

Here is a comparison taken directly from his comment on php.net :

Exception::getTraceAsString :

#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}

jTraceEx :

Exception: Thrown from class C
 at seabird.test.C.exc(index.php:78)
 at seabird.test.C.doexc(index.php:70)
 at seabird.test.fail2(index.php:85)
 at seabird.test.fail1(index.php:89)
 at (main)(index.php:93)
Caused by: Exception: Thrown from class B
 at seabird.test.B.exc(index.php:64)
 at seabird.test.C.exc(index.php:75)
 ... 4 more
Caused by: Exception: Thrown from class A
 at seabird.test.A.exc(index.php:46)
 at seabird.test.B.exc(index.php:61)
 ... 5 more
jJeQQOZ5 2024-08-22 13:45:24

打印回溯

debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

您可以使用It does not truncate

。打印示例是

#0  W3\\Sausage\\Mushroom->setCredentials() called at [/sausage/common/library/W3/Vzaar/Api.php:40]
#1  W3\\Sausage\\Mushroom->__construct() called at [/sausage/common/modules/video.mod.php:24]
#2  ModVideo->__construct() called at [/sausage/common/core/modules.core.php:133]
#3  Modules->__get() called at [/sausage/common/search/Classified/ESAdapter.php:399]
#4  Base\\Search\\Classified\\ESAdapter->getVideoInfo() called at [/sausage/common/search/Classified/ESAdapter.php:436]
#5  Base\\Search\\Classified\\ESAdapter->fillDataSet() called at [/sausage/common/search/Adapter.php:58]

You can print the backtrace with

debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

It does not truncate.

Example print would be

#0  W3\\Sausage\\Mushroom->setCredentials() called at [/sausage/common/library/W3/Vzaar/Api.php:40]
#1  W3\\Sausage\\Mushroom->__construct() called at [/sausage/common/modules/video.mod.php:24]
#2  ModVideo->__construct() called at [/sausage/common/core/modules.core.php:133]
#3  Modules->__get() called at [/sausage/common/search/Classified/ESAdapter.php:399]
#4  Base\\Search\\Classified\\ESAdapter->getVideoInfo() called at [/sausage/common/search/Classified/ESAdapter.php:436]
#5  Base\\Search\\Classified\\ESAdapter->fillDataSet() called at [/sausage/common/search/Adapter.php:58]
瞄了个咪的 2024-08-22 13:45:24

如果重新抛出异常,可以提供前一个异常作为第三个参数。通过这样做,可以链接异常跟踪。

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)

In case of rethrowing Exceptions it's possible to supply the previous Exception as the third argument. By doing so, it's possible to chain the Exception trace.

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;
}

OUTPUT:

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)
玩套路吗 2024-08-22 13:45:24

我(最近,经过很长一段时间,当我思考如何更好、尽可能准确和描述性地发出异常警告时)使用以下(可能不完美,但仍然可用)方法解决了 getTrace 报告不完整的问题:方法

private function Create_TraceText()
{
    $trace_text = "<b>TRACE:</b> <br /><br />\n";
    $trace = $this -> getTrace();

    for($index = 0; $index < count($trace); $index++)
    {
        $trace_text .= 'STEP ' . ($index + 1) . ":<br />\n";

        foreach($trace[$index] as $trace_name => $trace_value)
        {
            $trace_value = $this -> Convert_StringifyEmpty($trace_value);
            $trace_value = $this -> Convert_StringifyNull($trace_value);
            $trace_value = $this -> Convert_StringifyArray($trace_value);
            $trace_text .= strtoupper($trace_name == 'args' ? 'arguments' : $trace_name) . ': ' . $trace_value . "<br />\n";
        }

        $trace_text .= "<br />\n";
    }

    return $trace_text;
}

Create_TraceText 调用另外三个(也是)我自己的方法。这些方法具有以下目的:

  • 在没有设置参数的情况下插入替代文本
  • 用 NULL 字符串替换 NULL 值
  • 将参数的数组转换为字符串(用逗号作为 glue
  • 使代码更具可读性

我选择了私有可访问性,因为它由处理报告组装的方法在内部调用。但如果您愿意,您可以将其公开。

它迭代跟踪,获取每个步骤的项目(键及其值)并将它们转换为下面编写的形式的字符串上面

TRACE:

STEP 1:
FILE: A:\WWW\Kilometrovnik\Kilometrovnik.php
LINE: 166
FUNCTION: Execute
CLASS: VMaX\MarC\Assemblers\CodeGenerator
TYPE: ->
ARGUMENTS: not defined

的示例中只有一个步骤,但如果跟踪需要,可以重复(自动完成) 。


注意:

当然可以直接使用getTrace方法。我选择当前的方式来支持使代码更具可读性(并且可能更快 - 如果方法 getTrace 仅使用一次)。

另外,如果您愿意,您可以删除将 args 替换为 arguments(并且已写入 args),或者让跟踪项小写,如下所示默认情况下。

跟踪部分类和函数可以合并为方法。但这当然不是必需的。

示例来自我的本地主机私人测试项目(您的文件名可能不同)。

I (very recently, after long time when I thought how to have exception warnings better and as exact and descriptive as possible) solved problem with incomplete report of getTrace with following (maybe not perfect, but still usable) method:

private function Create_TraceText()
{
    $trace_text = "<b>TRACE:</b> <br /><br />\n";
    $trace = $this -> getTrace();

    for($index = 0; $index < count($trace); $index++)
    {
        $trace_text .= 'STEP ' . ($index + 1) . ":<br />\n";

        foreach($trace[$index] as $trace_name => $trace_value)
        {
            $trace_value = $this -> Convert_StringifyEmpty($trace_value);
            $trace_value = $this -> Convert_StringifyNull($trace_value);
            $trace_value = $this -> Convert_StringifyArray($trace_value);
            $trace_text .= strtoupper($trace_name == 'args' ? 'arguments' : $trace_name) . ': ' . $trace_value . "<br />\n";
        }

        $trace_text .= "<br />\n";
    }

    return $trace_text;
}

Method Create_TraceText calls three other methods that are (also) my own. Those methods have following purpose:

  • insert alternative text in case of no argument was set
  • replace NULL value with NULL string
  • convert arguments' array into string (with comma as glue)
  • make code better readable

I chose private accessibility because it is called internally by method that handles report assembling. But if you would like, you may have it public.

It iterates through trace, takes items (keys and their values) of each step and converts them into string in form that is written below

TRACE:

STEP 1:
FILE: A:\WWW\Kilometrovnik\Kilometrovnik.php
LINE: 166
FUNCTION: Execute
CLASS: VMaX\MarC\Assemblers\CodeGenerator
TYPE: ->
ARGUMENTS: not defined

There is only one step in example above, but it may be repeated (automatically done), if demanded by trace.


Notice:

Method getTrace may be used directly, of course. I chose current way to support make code better readable (and may be faster - if method getTrace is used only once).

Also, if you would like, you may delete replacement of args with arguments (and have written args) or let trace items lowercase, as is in default.

Trace parts class and function may be united into method. But it is not neccessary, of course.

Example comes from my localhost private testing project (and your file name may be different).

坏尐絯 2024-08-22 13:45:24

如果您记录的异常参数被截断,请

Str::replaceArray('?', Array, 'insert into `or...')

在 php.ini 中设置此参数并重新启动 php-fpm(如果您使用 Laravel Herd,请从菜单栏“显示 php.ini”+ 启动和停止)。这消除了异常跟踪中函数参数的截断限制。默认情况下,它将函数参数截断为 15 个字节。

zend.exception_string_param_max_len=1000000

请注意,0 也可能有效。请参阅 https:// /www.php.net/manual/en/ini.core.php#ini.zend.exception-string-param-max-len

现在,当您检查日志时,您会获得实际可用的数据:

Str::replaceArray('?', Array, 'insert into `table` (`type`, `name`, `description`, `article_number`, `price`, `vat_percentage`,`enabled_at`, `state`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')

In case your logged exceptions arguments are getting truncated like

Str::replaceArray('?', Array, 'insert into `or...')

Set this in your php.ini and restart php-fpm (if you're using Laravel Herd, from the menu bar "show php.ini" + start and stop). This removes the truncation limit on function arguments in exception traces. By default it truncates function parameters at 15 bytes.

zend.exception_string_param_max_len=1000000

Note that 0 might work too. See https://www.php.net/manual/en/ini.core.php#ini.zend.exception-string-param-max-len.

Now when you check your logs, you get actually usable data:

Str::replaceArray('?', Array, 'insert into `table` (`type`, `name`, `description`, `article_number`, `price`, `vat_percentage`,`enabled_at`, `state`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
孤星 2024-08-22 13:45:24

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

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

从 Andre 的这个伟大的答案中窃取

If you can get away with var_dump() an easy solution is:

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

Stolen from this great answer by Andre

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文