AirtimeLog.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. class Airtime_Zend_Log extends Zend_Log
  3. {
  4. /**
  5. *
  6. * @var boolean
  7. */
  8. protected $_registeredErrorHandler = false;
  9. /**
  10. *
  11. * @var array|boolean
  12. */
  13. protected $_errorHandlerMap = false;
  14. /**
  15. *
  16. * @var callback
  17. */
  18. protected $_origErrorHandler = null;
  19. public function __construct(Zend_Log_Writer_Abstract $writer = null)
  20. {
  21. parent::__construct($writer);
  22. }
  23. /**
  24. * Error Handler will convert error into log message, and then call the original error handler
  25. *
  26. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  27. * @param int $errno
  28. * @param string $errstr
  29. * @param string $errfile
  30. * @param int $errline
  31. * @param array $errcontext
  32. * @return boolean
  33. */
  34. public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  35. {
  36. $errorLevel = error_reporting();
  37. if ($errorLevel && $errno) {
  38. if (isset($this->_errorHandlerMap[$errno])) {
  39. $priority = $this->_errorHandlerMap[$errno];
  40. } else {
  41. $priority = Zend_Log::INFO;
  42. }
  43. $this->log($errstr, $priority, array('errno'=>$errno, 'file'=>$errfile, 'line'=>$errline, 'context'=>$errcontext));
  44. }
  45. if ($this->_origErrorHandler !== null) {
  46. return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);
  47. }
  48. return false;
  49. }
  50. /**
  51. * Register Logging system as an error handler to log php errors
  52. * Note: it still calls the original error handler if set_error_handler is able to return it.
  53. *
  54. * Errors will be mapped as:
  55. * E_NOTICE, E_USER_NOTICE => NOTICE
  56. * E_WARNING, E_CORE_WARNING, E_USER_WARNING => WARN
  57. * E_ERROR, E_USER_ERROR, E_CORE_ERROR, E_RECOVERABLE_ERROR => ERR
  58. * E_DEPRECATED, E_STRICT, E_USER_DEPRECATED => DEBUG
  59. * (unknown/other) => INFO
  60. *
  61. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  62. *
  63. * @return Zend_Log
  64. */
  65. public function registerErrorHandler()
  66. {
  67. // Only register once. Avoids loop issues if it gets registered twice.
  68. if ($this->_registeredErrorHandler) {
  69. return $this;
  70. }
  71. $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler'));
  72. // Contruct a default map of phpErrors to Zend_Log priorities.
  73. // Some of the errors are uncatchable, but are included for completeness
  74. $this->_errorHandlerMap = array(
  75. E_NOTICE => Zend_Log::NOTICE,
  76. E_USER_NOTICE => Zend_Log::NOTICE,
  77. E_WARNING => Zend_Log::WARN,
  78. E_CORE_WARNING => Zend_Log::WARN,
  79. E_USER_WARNING => Zend_Log::WARN,
  80. E_ERROR => Zend_Log::ERR,
  81. E_USER_ERROR => Zend_Log::ERR,
  82. E_CORE_ERROR => Zend_Log::ERR,
  83. E_RECOVERABLE_ERROR => Zend_Log::ERR,
  84. E_STRICT => Zend_Log::DEBUG,
  85. );
  86. // PHP 5.3.0+
  87. if (defined('E_DEPRECATED')) {
  88. $this->_errorHandlerMap['E_DEPRECATED'] = Zend_Log::DEBUG;
  89. }
  90. if (defined('E_USER_DEPRECATED')) {
  91. $this->_errorHandlerMap['E_USER_DEPRECATED'] = Zend_Log::DEBUG;
  92. }
  93. $this->_registeredErrorHandler = true;
  94. return $this;
  95. }
  96. }