我正在使用 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/