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

标签 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...')

如何打印完整的字符串?

最佳答案

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

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

关于php - 如何获取 PHP 的 getTraceAsString() 的完整字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1949345/

相关文章:

php - 使用短代码从数据库中检索数据

java - 使用多个正则表达式解析字符串

javascript - Node 不抛出堆栈溢出异常

java - 使用 try/catch 将数字存储到用户定义的数组中,并在最后一个数组索引中继续

c# - 倾斜动画无法制作动画

javascript - 通过ajax传输数据到php

php - 从 php&code 插入时,数据没有出现在数据库中

php - 记录有意切换失败的最佳方法是什么?

c++ - 在 C++ 中解析字符串

Java contains() 方法无法正常工作