From aa641e5169aaf71a53ac0df87ee1112cdf41fad8 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Mon, 17 Feb 2025 12:30:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootstrap/ConsoleErrorHandler.php | 32 ++++++++++++++++++++++++++++++- bootstrap/HttpErrorHandler.php | 29 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/bootstrap/ConsoleErrorHandler.php b/bootstrap/ConsoleErrorHandler.php index 20aacad1..daf794f5 100644 --- a/bootstrap/ConsoleErrorHandler.php +++ b/bootstrap/ConsoleErrorHandler.php @@ -11,6 +11,7 @@ use App\Library\Logger as AppLogger; use Phalcon\Config as PhConfig; use Phalcon\Di\Injectable; use Phalcon\Logger\Adapter\File as PhLogger; +use Throwable; class ConsoleErrorHandler extends Injectable { @@ -18,10 +19,14 @@ class ConsoleErrorHandler extends Injectable public function __construct() { set_exception_handler([$this, 'handleException']); + + set_error_handler([$this, 'handleError']); + + register_shutdown_function([$this, 'handleShutdown']); } /** - * @param \Throwable $e + * @param Throwable $e */ public function handleException($e) { @@ -45,6 +50,31 @@ class ConsoleErrorHandler extends Injectable echo $content . PHP_EOL; } + public function handleError($errNo, $errStr, $errFile, $errLine) + { + if (in_array($errNo, [E_WARNING, E_NOTICE, E_DEPRECATED, E_USER_WARNING, E_USER_NOTICE, E_USER_DEPRECATED])) { + return true; + } + + $logger = $this->getLogger(); + + $logger->error("Error [{$errNo}]: {$errStr} in {$errFile} on line {$errLine}"); + + return false; + } + + public function handleShutdown() + { + $error = error_get_last(); + + if ($error !== NULL && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { + + $logger = $this->getLogger(); + + $logger->error("Fatal Error [{$error['type']}]: {$error['message']} in {$error['file']} on line {$error['line']}"); + } + } + /** * @return PhConfig */ diff --git a/bootstrap/HttpErrorHandler.php b/bootstrap/HttpErrorHandler.php index 83379c01..e7f3b1ad 100644 --- a/bootstrap/HttpErrorHandler.php +++ b/bootstrap/HttpErrorHandler.php @@ -23,6 +23,10 @@ class HttpErrorHandler extends Injectable public function __construct() { set_exception_handler([$this, 'handleException']); + + set_error_handler([$this, 'handleError']); + + register_shutdown_function([$this, 'handleShutdown']); } /** @@ -45,6 +49,31 @@ class HttpErrorHandler extends Injectable } } + public function handleError($errNo, $errStr, $errFile, $errLine) + { + if (in_array($errNo, [E_WARNING, E_NOTICE, E_DEPRECATED, E_USER_WARNING, E_USER_NOTICE, E_USER_DEPRECATED])) { + return true; + } + + $logger = $this->getLogger(); + + $logger->error("Error [{$errNo}]: {$errStr} in {$errFile} on line {$errLine}"); + + return false; + } + + public function handleShutdown() + { + $error = error_get_last(); + + if ($error !== NULL && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) { + + $logger = $this->getLogger(); + + $logger->error("Fatal Error [{$error['type']}]: {$error['message']} in {$error['file']} on line {$error['line']}"); + } + } + /** * @param Throwable $e */