如何获取 PHP getTraceAsString() 的完整字符串?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我创建了这个函数来返回没有截断字符串的堆栈跟踪:
或者,您可以编辑截断输出的 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:
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
一些更好的版本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.
该解决方案很好,但就我而言,它引发了错误,因为我的跟踪中有内部函数。我添加了几行代码来检查这一点,以便跟踪功能仍然有效。
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.
更改 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()
还有 Ernest Vogelsinger 的优秀
jTraceEx
配方,位于 https://www.php.net/manual/exception.gettraceasstring.php#114980,支持链式异常,并以类似 Java 的方式格式化。以下是直接取自他对 php.net 的评论的比较:
Exception::getTraceAsString :
jTraceEx :
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 :
jTraceEx :
打印回溯
您可以使用It does not truncate
。打印示例是
You can print the backtrace with
It does not truncate.
Example print would be
如果重新抛出异常,可以提供前一个异常作为第三个参数。通过这样做,可以链接异常跟踪。
输出:
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.
OUTPUT:
我(最近,经过很长一段时间,当我思考如何更好、尽可能准确和描述性地发出异常警告时)使用以下(可能不完美,但仍然可用)方法解决了 getTrace 报告不完整的问题:方法
Create_TraceText
调用另外三个(也是)我自己的方法。这些方法具有以下目的:我选择了私有可访问性,因为它由处理报告组装的方法在内部调用。但如果您愿意,您可以将其公开。
它迭代跟踪,获取每个步骤的项目(键及其值)并将它们转换为下面编写的形式的字符串上面
的示例中只有一个步骤,但如果跟踪需要,可以重复(自动完成) 。
注意:
当然可以直接使用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:
Method
Create_TraceText
calls three other methods that are (also) my own. Those methods have following purpose: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
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
witharguments
(and have writtenargs
) 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).
如果您记录的异常参数被截断,请
在 php.ini 中设置此参数并重新启动 php-fpm(如果您使用 Laravel Herd,请从菜单栏“显示 php.ini”+ 启动和停止)。这消除了异常跟踪中函数参数的截断限制。默认情况下,它将函数参数截断为 15 个字节。
请注意,
0
也可能有效。请参阅 https:// /www.php.net/manual/en/ini.core.php#ini.zend.exception-string-param-max-len。现在,当您检查日志时,您会获得实际可用的数据:
In case your logged exceptions arguments are getting truncated like
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.
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:
如果您可以使用
var_dump()
,一个简单的解决方案是:从 Andre 的这个伟大的答案中窃取
If you can get away with
var_dump()
an easy solution is:Stolen from this great answer by Andre