在技术开发中,捕获程序框架错误,是非常必要的一件事情,我们公司使用Yii2框架,简单说下Yii2的错误捕获处理
Yii2 web应用
1 配置如下
其中errorHandler就是错误处理配置,执行ErrorController的actionError
'components' => [ "urlManager" => require (__DIR__ . '/router.php'), 'errorHandler' => [ 'errorAction' => 'error/error', ], ]
2 ErrorContrller代码示例
<?php namespace blogcontrollers; use commonserviceapplogApplogService; use Yii; class ErrorController extends yiiwebController{ public $enableCsrfValidation = false; public function actionError() { $code = 404; $msg = 'Page Not Found'; $error = Yii::$app->errorHandler->exception; if ($error) { $code = $error->getCode(); $msg = $error->getMessage(); $file = $error->getFile(); $line = $error->getLine(); $err_msg = $msg . " [file: {$file}][line: {$line}][err code:$code.][url:{$_SERVER['REQUEST_URI']}][post:".http_build_query($_POST)."]"; ApplogService::add(Yii::$app->id,$err_msg);//我这里存入数据库 } } }
命令行应用
1 配置如下,配置指定使用ErrorController(命令行应用和web应用还是有区别的)
'components' => [ "urlManager" => require (__DIR__ . '/router.php'), 'errorHandler' => [ 'class' => 'consolecontrollersErrorController' ] ]
2 ErrorController代码示例
<?php namespace consolecontrollers;; use commonserviceapplogApplogService; use Yii; use yiiaseErrorException; use yiiaseUserException; use yiihelpersConsole; use yiiconsoleException; class ErrorController extends yiiaseErrorHandler{ protected function renderException($exception){ if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) { $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage(); } else{ if ($exception instanceof Exception) { $message = $this->formatMessage("Exception ({$exception->getName()})"); } elseif ($exception instanceof ErrorException) { $message = $this->formatMessage($exception->getName()); } else { $message = $this->formatMessage('Exception'); } $message .= $this->formatMessage(" '" . get_class($exception) . "'", [Console::BOLD, Console::FG_BLUE]) . " with message " . $this->formatMessage("'{$exception->getMessage()}'", [Console::BOLD]) //. " " . " in " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD]) . ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . " "; if ($exception instanceof yiidbException && !empty($exception->errorInfo)) { $message .= " " . $this->formatMessage("Error Info: ", [Console::BOLD]) . print_r($exception->errorInfo, true); } $message .= " " . $this->formatMessage("Stack trace: ", [Console::BOLD]) . $exception->getTraceAsString(); } ApplogService::add(Yii::$app->id,$message);//存到数据库中 if (PHP_SAPI === 'cli') { Console::stderr($message . " "); } else { echo $message . " "; } } protected function formatMessage($message, $format = [Console::FG_RED, Console::BOLD]){ $stream = (PHP_SAPI === 'cli') ? STDERR : STDOUT; // try controller first to allow check for --color switch if (Yii::$app->controller instanceof yiiconsoleController && Yii::$app->controller->isColorEnabled($stream) || Yii::$app instanceof yiiconsoleApplication && Console::streamSupportsAnsiColors($stream)) { $message = Console::ansiFormat($message, $format); } return $message; } }
对比下 命令行应用 和 web应用,ErrorController继承的不一样,并且实现的方法也不一样的
原文地址:Yii2 捕获错误日志
标签:yii2 console error capture