开发者

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

开发者 https://www.devze.com 2023-02-18 02:59 出处:网络
I have configured php to log errors and on my development machine, they show up in the apache error logs as:

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

[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 开发者_JAVA技巧look like:

[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.


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.

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.

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.


You can trace by using debug_backtrace or debug_print_backtrace though I never used them. Best tracing come from from xdebug or Zend debugger. I agree with Gordon that you should not install a debugger on a production machine.

0

精彩评论

暂无评论...
验证码 换一张
取 消