2012-05-06 04:28:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Airtime_Zend_Log extends Zend_Log
|
|
|
|
{
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* @var bool
|
2012-05-06 04:28:52 +02:00
|
|
|
*/
|
|
|
|
protected $_registeredErrorHandler = false;
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-05-06 04:28:52 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* @var array|bool
|
2012-05-06 04:28:52 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
protected $_errorHandlerMap = false;
|
|
|
|
|
2012-05-06 04:28:52 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* @var callable
|
2012-05-06 04:28:52 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
protected $_origErrorHandler;
|
|
|
|
|
2024-04-13 14:36:31 +02:00
|
|
|
public function __construct(?Zend_Log_Writer_Abstract $writer = null)
|
2012-05-06 04:28:52 +02:00
|
|
|
{
|
|
|
|
parent::__construct($writer);
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-05-06 04:28:52 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Error Handler will convert error into log message, and then call the original error handler.
|
|
|
|
*
|
2022-08-25 16:25:54 +02:00
|
|
|
* @see https://www.php.net/manual/en/function.set-error-handler.php Custom error handler
|
2012-05-06 04:28:52 +02:00
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param int $errno
|
2012-05-06 04:28:52 +02:00
|
|
|
* @param string $errstr
|
|
|
|
* @param string $errfile
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param int $errline
|
|
|
|
* @param array $errcontext
|
|
|
|
*
|
|
|
|
* @return bool
|
2012-05-06 04:28:52 +02:00
|
|
|
*/
|
|
|
|
public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
|
|
|
|
{
|
|
|
|
$errorLevel = error_reporting();
|
|
|
|
|
|
|
|
if ($errorLevel && $errno) {
|
|
|
|
if (isset($this->_errorHandlerMap[$errno])) {
|
|
|
|
$priority = $this->_errorHandlerMap[$errno];
|
|
|
|
} else {
|
|
|
|
$priority = Zend_Log::INFO;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->log($errstr, $priority, ['errno' => $errno, 'file' => $errfile, 'line' => $errline, 'context' => $errcontext]);
|
2012-05-06 04:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_origErrorHandler !== null) {
|
|
|
|
return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-05-06 04:28:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-05-06 04:28:52 +02:00
|
|
|
/**
|
|
|
|
* Register Logging system as an error handler to log php errors
|
|
|
|
* Note: it still calls the original error handler if set_error_handler is able to return it.
|
|
|
|
*
|
|
|
|
* Errors will be mapped as:
|
|
|
|
* E_NOTICE, E_USER_NOTICE => NOTICE
|
|
|
|
* E_WARNING, E_CORE_WARNING, E_USER_WARNING => WARN
|
|
|
|
* E_ERROR, E_USER_ERROR, E_CORE_ERROR, E_RECOVERABLE_ERROR => ERR
|
|
|
|
* E_DEPRECATED, E_STRICT, E_USER_DEPRECATED => DEBUG
|
|
|
|
* (unknown/other) => INFO
|
|
|
|
*
|
2022-08-25 16:25:54 +02:00
|
|
|
* @see https://www.php.net/manual/en/function.set-error-handler.php Custom error handler
|
2012-05-06 04:28:52 +02:00
|
|
|
*
|
|
|
|
* @return Zend_Log
|
|
|
|
*/
|
|
|
|
public function registerErrorHandler()
|
|
|
|
{
|
|
|
|
// Only register once. Avoids loop issues if it gets registered twice.
|
|
|
|
if ($this->_registeredErrorHandler) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->_origErrorHandler = set_error_handler([$this, 'errorHandler']);
|
2012-05-06 04:28:52 +02:00
|
|
|
|
|
|
|
// Contruct a default map of phpErrors to Zend_Log priorities.
|
|
|
|
// Some of the errors are uncatchable, but are included for completeness
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->_errorHandlerMap = [
|
|
|
|
E_NOTICE => Zend_Log::NOTICE,
|
|
|
|
E_USER_NOTICE => Zend_Log::NOTICE,
|
|
|
|
E_WARNING => Zend_Log::WARN,
|
|
|
|
E_CORE_WARNING => Zend_Log::WARN,
|
|
|
|
E_USER_WARNING => Zend_Log::WARN,
|
|
|
|
E_ERROR => Zend_Log::ERR,
|
|
|
|
E_USER_ERROR => Zend_Log::ERR,
|
|
|
|
E_CORE_ERROR => Zend_Log::ERR,
|
2012-05-06 04:28:52 +02:00
|
|
|
E_RECOVERABLE_ERROR => Zend_Log::ERR,
|
2021-10-11 16:10:47 +02:00
|
|
|
E_STRICT => Zend_Log::DEBUG,
|
|
|
|
];
|
2012-05-06 04:28:52 +02:00
|
|
|
// PHP 5.3.0+
|
|
|
|
if (defined('E_DEPRECATED')) {
|
|
|
|
$this->_errorHandlerMap['E_DEPRECATED'] = Zend_Log::DEBUG;
|
|
|
|
}
|
|
|
|
if (defined('E_USER_DEPRECATED')) {
|
|
|
|
$this->_errorHandlerMap['E_USER_DEPRECATED'] = Zend_Log::DEBUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_registeredErrorHandler = true;
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2012-05-06 04:28:52 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|