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

Use*_*ser 20 php string exception truncation truncated

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

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

catch (SoapFault $e) {
error_log( $e->getTraceAsString() )
}
Run Code Online (Sandbox Code Playgroud)

打印出来的字符串是:

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

如何打印完整的字符串?

Ste*_*eve 30

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢你 但是,当我尝试它时,我得到两个通知:`Notice:Undefined index:file`和`Notice:Undefined index:line`。 (2认同)

Lad*_*vec 5

一些更好的版本/sf/answers/425366721/在这里https://gist.github.com/1437966添加了类输出.


小智 5

该解决方案很好,但是在我的情况下,由于跟踪中具有内部功能,因此引发了错误。我添加了几行代码进行检查,因此跟踪功能仍然有效。

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;
}
Run Code Online (Sandbox Code Playgroud)


Gor*_*don 3

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

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

  • 在创建这篇文章之前,我将 log_errors_max_len 的值从 1024 增加到 4096。但是没有区别。 (3认同)