如何让 PHP 在发生致命错误时记录堆栈跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5350070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:06:54  来源:igfitidea点击:

How do I get PHP to log a stacktrace upon fatal error

php

提问by troelskn

I have configured php to log errors and on my development machine, they show up in the apache error logs as:

我已将 php 配置为记录错误,并且在我的开发机器上,它们在 apache 错误日志中显示为:

[Thu Mar 17 18:22:07 2011] [error] [client ::1] PHP Parse error:  syntax error, unexpected ')' in /Users/troelskn/Projects/test/bootstrap.inc.php on line 27
[Thu Mar 17 18:22:07 2011] [error] [client ::1] PHP Stack trace:
[Thu Mar 17 18:22:07 2011] [error] [client ::1] PHP   1. {main}() /Users/troelskn/Projects/test/public/index.php:0

However, on the production machines (Ubuntu) there is no stacktrace following the error and there is a referrer attached to the message. Eg. it would look like:

但是,在生产机器 (Ubuntu) 上,错误后没有堆栈跟踪,并且消息附加了引用。例如。它看起来像:

[Thu Mar 17 18:22:07 2011] [error] [client ::1] PHP Parse error:  syntax error, unexpected ')' in /Users/troelskn/Projects/test/bootstrap.inc.php on line 27, referer: http://localhost/

How can I control this formatting? I would very much like to have the stacktrace available in the logs.

如何控制这种格式?我非常希望在日志中提供堆栈跟踪。

采纳答案by GordonM

Well, as has already been pointed out, you're not getting the same formatting on the live machine because the live machine hasn't got xdebug installed. There is debug_backtrace but that wouldn't catch a fatal error.

好吧,正如已经指出的那样,您在实时机器上没有得到相同的格式,因为实时机器没有安装 xdebug。有 debug_backtrace 但这不会捕获致命错误。

You could install xdebug on the live server, but you'd have to be very careful to configure it to expose no functionality except for the stack trace logging. Incautious use of xdebug on a live box could pose a security risk as people could initiate a remote debug session, or its enhanced error messages could inadvertently echo out internal details of your code.

您可以在实时服务器上安装 xdebug,但您必须非常小心地将其配置为除了堆栈跟踪日志记录外不公开任何功能。在 live box 上不谨慎地使用 xdebug 可能会带来安全风险,因为人们可能会启动远程调试会话,或者其增强的错误消息可能会无意中反映您代码的内部细节。

To be honest? I'd think your best option is to try and recreate the circumstances under which the error the live server logged occurred on your test server.

老实说?我认为您最好的选择是尝试重新创建实时服务器记录的错误发生在您的测试服务器上的情况。

EDIT TO ADD: Forgot to mention that in addition to being a security risk, xDebug will also have a negative impact on your website's performance. It hooks into Zend Engine in several crucial ways to log programme state and alter its behaviour (such as overriding @ error suppression), and this will have an inevitable impact on performance. You're basically far better off trying to replicate the issue on a testing environment than you are adding debugging tools to a live one.

编辑添加:忘记提及除了存在安全风险之外,xDebug 还会对您网站的性能产生负面影响。它以几种关键方式挂接到 Zend 引擎,以记录程序状态并改变其行为(例如覆盖@错误抑制),这将对性能产生不可避免的影响。与在实时环境中添加调试工具相比,尝试在测试环境中复制问题基本上要好得多。

回答by Elzo Valugi

You can trace by using debug_backtraceor debug_print_backtracethough I never used them. Best tracing come from from xdebugor Zend debugger. I agree with Gordon that you should not install a debugger on a production machine.

您可以使用debug_backtracedebug_print_backtrace进行跟踪,尽管我从未使用过它们。最好的跟踪来自xdebugZend debugger。我同意 Gordon 的观点,你不应该在生产机器上安装调试器。