3
\$\begingroup\$

Introduction

I wrote three functions that aims to log errors and exceptions as well as show a user a generic error page. Would appreciate some feedback on it with regards to the PSR standards and if I have left out anything of value.

Memory issue:

I am aware that the memory issue has not been addressed in this code, i.e. if you run out of memory the errors will not be logged. However, this isn't an issue I'm looking to solve at the moment.

Code:

<?php
 // General error handling
 error_reporting(-1);
 ini_set('display_errors', 0);
 ini_set('log_errors', 1);
 // Custom error handler
 function errorHandler($type, $str, $file, $line) {
 // Sets path to error log
 $errorLog = "/home/ubuntu/errors.log";
 // Generic error structure
 $msg = $str . ' in ' . $file . ' on line ' . $line;
 // Checks error severity
 switch ($type) {
 // Notices
 case E_NOTICE:
 case E_USER_NOTICE:
 case E_DEPRECATED:
 case E_USER_DEPRECATED:
 case E_STRICT:
 // Logs notices to file
 $notice = 'NOTICE ' . $msg;
 file_put_contents($errorLog, $notice . PHP_EOL, FILE_APPEND);
 break;
 // Warnings
 case E_WARNING:
 case E_USER_WARNING:
 // Logs warnings to file
 $warning = 'WARNING ' . $msg;
 file_put_contents($errorLog, $warning . PHP_EOL, FILE_APPEND);
 break;
 // Unknown errors
 default:
 // Sets header and redirect user to generic error page
 header('HTTP/1.1 500 Internal Server Error', true, 500);
 require_once('500.php');
 // Terminates the script
 exit;
 }
 }
 // Custom exception handler
 function exceptionHandler($e) {
 // Sets path to exception log
 $exceptionLog = "/home/ubuntu/exception.log";
 // Logs exceptions to file
 file_put_contents($exceptionLog, $e->__toString() . PHP_EOL, FILE_APPEND);
 // Sets header and redirect user to generic error page
 header('HTTP/1.1 500 Internal Server Error', true, 500);
 require_once('500.php');
 // Terminates the script
 exit;
 }
 // Logs on shutdown caused by an error
 function fatalErrorHandler() {
 // Gets the last occured error
 $error = error_get_last();
 // Check if error exists
 if (!empty($error) && in_array($error['type'], array(E_ERROR, E_USER_ERROR))) {
 // Sets path to error log
 $fatalErrorLog = "/home/ubuntu/errors.log";
 // Generic fatal error structure
 $fatal = 'FATAL ' . $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'];
 // Logs fatal error to file
 file_put_contents($fatalErrorLog, $fatal . PHP_EOL, FILE_APPEND);
 // Sets header and redirect user to generic error page
 header('HTTP/1.1 500 Internal Server Error', true, 500);
 require_once('500.php');
 // Terminates the script
 exit;
 }
 }
 // Sets custom handler for errors, exceptions and fatal errors
 set_error_handler('errorHandler');
 set_exception_handler('exceptionHandler');
 register_shutdown_function('fatalErrorHandler');
?>
Mathieu Guindon
75.5k18 gold badges194 silver badges467 bronze badges
asked Sep 6, 2015 at 13:22
\$\endgroup\$
2
  • \$\begingroup\$ I've reworded the part about the known issue, as asking the CR community for help fixing specific programming issues is out of scope (that's SO's territory ;-) \$\endgroup\$ Commented Sep 6, 2015 at 13:39
  • \$\begingroup\$ @Mat'sMug Of course, was not my intention. Thanks for the edit. \$\endgroup\$ Commented Sep 6, 2015 at 13:40

1 Answer 1

2
\$\begingroup\$

The first thing which comes to my mind when reading your code is how concurrent requests with errors can tamper with the written data. You can aqquire an exclusive lock on the file for the duration of the writing with the flag LOCK_EX (see file_put_contents documentation).

I would also make sure the file 500.php actually exists. You could also avoid using require_once since the script is terminated afterwards and the possibility for another include is non-existent.

I assume the comments are because the code is posted here, since none of them provide extra information your code doesn't provide. Otherwise I think your code look fine. The variables have meaningful names and the indentation is good.

Happy coding!

answered Sep 8, 2015 at 11:47
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.