Logging fatal errors

suggest change

In PHP, a fatal error is a kind of error that cannot be caught, that is, after experiencing a fatal error a program does not resume. However, to log this error or somehow handle the crash you can use register_shutdown_function to register shutdown handler.

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\n",date("Y-m-d H:i:s"),$type,$message,$file,$line);fclose($logFile);}register_shutdown_function('fatalErrorHandler');

Reference:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Exception handling and error reporting:
* Logging fatal errors

Table Of Contents
2 Arrays
4 Types
6 Exception handling and error reporting
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR