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');
?>
\$\endgroup\$
  • \$\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\$ – Mathieu Guindon Sep 6 '15 at 13:39
  • \$\begingroup\$ @Mat'sMug Of course, was not my intention. Thanks for the edit. \$\endgroup\$ – kexxcream Sep 6 '15 at 13:40
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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.