ErrorHandler.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Tools\Error;
  3. use Cake\Error\ErrorHandler as CoreErrorHandler;
  4. use Cake\Log\Log;
  5. use Exception;
  6. use Tools\Error\Middleware\ErrorHandlerMiddleware;
  7. /**
  8. * Custom ErrorHandler to not mix the 404 exceptions with the rest of "real" errors in the error.log file.
  9. *
  10. * All you need to do is:
  11. * - switch `use Cake\Error\ErrorHandler;` with `use Tools\Error\ErrorHandler;` in your bootstrap
  12. * - Make sure you got the 404 log defined either in your app.php or as Log::config() call.
  13. *
  14. * Example config as scoped one:
  15. * - 'className' => 'Cake\Log\Engine\FileLog',
  16. * - 'path' => LOGS,
  17. * - 'file'=> '404',
  18. * - 'levels' => ['error'],
  19. * - 'scopes' => ['404']
  20. *
  21. * If you don't want the errors to also show up in the debug and error log, make sure you set
  22. * `'scopes' => false` for those two in your app.php file.
  23. *
  24. * In case you need custom 404 mappings for some additional custom exceptions, make use of `log404` option.
  25. * It will overwrite the current defaults completely.
  26. *
  27. * @deprecated As of CakePHP 3.3+. Use Tools\Error\Middleware\ErrorHandlerMiddleware now.
  28. */
  29. class ErrorHandler extends CoreErrorHandler {
  30. /**
  31. * Handles exception logging
  32. *
  33. * @param \Exception $exception Exception instance.
  34. * @return bool
  35. */
  36. protected function _logException(Exception $exception) {
  37. $blacklist = ErrorHandlerMiddleware::$blacklist;
  38. if (isset($this->_options['log404'])) {
  39. $blacklist = $this->_options['log404'];
  40. }
  41. if ($blacklist && in_array(get_class($exception), (array)$blacklist)) {
  42. $level = LOG_ERR;
  43. Log::write($level, $this->_getMessage($exception), ['404']);
  44. return false;
  45. }
  46. return parent::_logException($exception);
  47. }
  48. }