记录致命错误

在 PHP 中,致命错误是一种无法捕获的错误,即在遇到致命错误后程序无法恢复。但是,要记录此错误或以某种方式处理崩溃,你可以使用 register_shutdown_function 注册关闭处理程序。

function fatalErrorHandler() {
    // Let's get last error that was fatal.
    $error = error_get_last();
   
    // This is error-only handler for example purposes; no error means that
    // there were no error and shutdown was proper. Also ensure it will handle 
    // only fatal errors.
    if (null === $error || E_ERROR != $error['type']) {
        return;
    }

    // Log last error to a log file.
    // let's naively assume that logs are in the folder inside the app folder.
    $logFile = fopen("./app/logs/error.log", "a+");

    // Get useful info out of error.
    $type    = $error["type"];
    $file    = $error["file"];
    $line    = $error["line"];
    $message = $error["message"]

    fprintf(
        $logFile,
        "[%s] %s: %s in %s:%d
",
        date("Y-m-d H:i:s"),
        $type,        
        $message,
        $file, 
        $line);

    fclose($logFile);       
}

register_shutdown_function('fatalErrorHandler');

参考: