10

I want to log every fatal errors, even timeout and others E_ERRORS ones. I use set_error_handler and shutdown function, but with the last one I cannot have the stacktrace. Is there any way to have it?

I want to log fatal errors on a production server to help resolving bugs which occurs only in production. I know, xdebug on dev servers must be enough, but it is not. Maybe we can use xdebug with the minimum of options activated, or a stripped version of it to add stack trace to error log?

This code print error information if one error occur, even a timeout.

<?php
function shutdown()
{
    $a=error_get_last();
    if($a!==null) print_r($a);  
}
register_shutdown_function('shutdown');

3 Answers 3

6

You won't get a stack trace on the fatal error because "FATAL" means script execution stops immediately, i.e. the trace can't be generated through the usual channels. So, if you have set a custom error handler to throw exceptions, it won't be invoked for fatal errors as per the PHP manual:

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

The easiest way to get info about what happened in the fatal error (short of turning display_errors on, which isn't an option in production environments) is to manually build the error information yourself in the shutdown handler. Also, I wouldn't use xdebug on a production server ... it's for debugging your development environment and will add unnecessary overhead in a production environment.

So, modifying your shutdown handler you could do something like this:

function shutdown()
{
  if ( ! $err = error_get_last()) {
    return;
  }

  $fatals = array(
    E_USER_ERROR      => 'Fatal Error',
    E_ERROR           => 'Fatal Error',
    E_PARSE           => 'Parse Error', 
    E_CORE_ERROR      => 'Core Error',
    E_CORE_WARNING    => 'Core Warning',
    E_COMPILE_ERROR   => 'Compile Error',
    E_COMPILE_WARNING => 'Compile Warning'
  );
  
  if (isset($fatals[$err['type']])) {
    $msg = $fatals[$err['type']] . ': ' . $err['message'] . ' in ';
    $msg.= $err['file'] . ' on line ' . $err['line'];
    error_log($msg);
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Note that I threw in the E_USER_ERROR in the $fatals array, but it can actually be handled successfully by a custom error handler.
Ok, I was thinking of a PHP module wich just catch the stacktrace for fatal errors, like xdebug but lighter. My quest is how to have more informations about fatal errors in production to help to correct them.
@CédricGirard Ah I see. I think the vast majority of fatal error are of the type that can be weeded out during the development phase ... the rest are extraordinary conditions (like running out of memory, smashing your server with a cudgel in mid-request, etc). In these cases I've always found the above method supplies sufficient information for logging. I haven't tested to see if it works with FATAL errors (I think it should), but you could try adding a call to debug_print_backtrace inside your shutdown handler for trace info
I try it, but it is always empty, as the shutdown handler is run after exécution of faulty code (and not as an exception). In my company we have a huge load of legacy applications, not all of them crafted with good practices, so I search for others ways to improve production quality.
3

If you use HHVM you can set

hhvm.error_handling.call_user_handler_on_fatals = 1

in your php.ini to have it call the error handler (with a stack trace) on fatal errors. (more info)

Using a profiler might also be an option - e.g. use XHProf and calling xhprof_disable() in the fatal handler might get you something remotely resembling a stack trace. (No idea if this actually works.)

1 Comment

The HHVM option and link are exactly what I was looking for.
1

For catching max execution timeouts you can use php-fpm and set slowlog + request_slowlog_timeout which will log full stack traces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.