Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 810
troelskn Avatar asked Mar 18 '11 09:03

troelskn


People also ask

How do I get PHP to produce a Backtrace upon error?

For more advanced solution, you can use XDebug extension for PHP. By default when XDebug is loaded, it should show you automatically the backtrace in case of any fatal error. Or you trace into file (xdebug. auto_trace) to have a very big backtrace of the whole request or do the profiling (xdebug.

Should Stacktrace be logged?

Therefore, you should log a stacktrace if, and only if, and always if, the exception indicates a bug in the program. However, that does not always indicate that a method you write should catch and log the exception.

What is Stacktrace PHP?

But, what is a stack trace? In essence, it is a rundown of every file and function that is called leading up to the error. To be clear, a stack trace doesn't include the files and functions that are touched before the error occurred, only the chain of methods that are called as the error happened.


2 Answers

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.

like image 65
GordonM Avatar answered Oct 10 '22 12:10

GordonM


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.

like image 36
Elzo Valugi Avatar answered Oct 10 '22 13:10

Elzo Valugi