0

How is it possible to log when/if PHP exceed max_execution_time and execution is terminated?

I already have these two functions

register_shutdown_function(function(){
    if($error = error_get_last()){
        switch($error['type']){
            case E_ERROR:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
                $message = $error['message'].' '.$error['file'].'('.$error['line'].')';
                if(class_exists('Log')){
                    Log::write($message, 'fatal', true, true);
                }
                if(ENV != ENV_PROD){
                    echo $message;
                }
                break;
        }
    }
});

set_error_handler(function(int $errno, string $errstr, string $errfile, int $errline){
    switch($errno){
        case E_WARNING:
        case E_PARSE:
        case E_NOTICE:
            $message = "$errstr $errfile($errline)\n".(new Error)->getTraceAsString();
            if(class_exists('Log')){
                Log::write($message, 'warning', true, true);
            }
            if(ENV != ENV_PROD){
                echo $message;
            }
            break;
    }
});
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • 1
    This is not possible in a direct manner, since per definition the script cannot execute any logging while it is terminated. You can use a log file analyzer to try to interpret the http status codes of requests or you have to implement a life beat that logs regularely. – arkascha Dec 29 '16 at 12:55
  • 2
    http://stackoverflow.com/a/11704803/427387 may be of use; also the php error log should show you that an execution time was exceeded. – Spechal Dec 29 '16 at 13:01
  • @Spechal The OP says they have already tried register_shutdown_function. I'm tempted to mark the question as duplicate though. – IMSoP Dec 29 '16 at 13:13
  • Possible duplicate of [How to catch the fatal error: Maximum execution time of 30 seconds exceeded in PHP](https://stackoverflow.com/questions/6861033/how-to-catch-the-fatal-error-maximum-execution-time-of-30-seconds-exceeded-in-p) – mleko May 27 '17 at 06:07

0 Answers0