Yii2 捕获错误日志
在技术开发中,捕获程序框架错误,是非常必要的一件事情,我们公司使用Yii2框架,简单说下Yii2的错误捕获处理
Yii2 web应用
1 配置如下
其中errorHandler就是错误处理配置,执行ErrorController的actionError
'components' => [
"urlManager" => require (__DIR__ . '/router.php'),
'errorHandler' => [
'errorAction' => 'error/error',
],
]
2 ErrorContrller代码示例
<?php namespace blog\controllers; use common\service\applog\ApplogService;
use Yii; class ErrorController extends \yii\web\Controller{
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' => 'console\controllers\ErrorController'
]
]
2 ErrorController代码示例
<?php namespace console\controllers;; use common\service\applog\ApplogService;
use Yii;
use yii\base\ErrorException;
use yii\base\UserException;
use yii\helpers\Console;
use yii\console\Exception; class ErrorController extends \yii\base\ErrorHandler{ 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]) //. "\n"
. "\n\nin " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD])
. ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . "\n";
if ($exception instanceof \yii\db\Exception && !empty($exception->errorInfo)) {
$message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . print_r($exception->errorInfo, true);
}
$message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString();
} ApplogService::add(Yii::$app->id,$message);//存到数据库中 if (PHP_SAPI === 'cli') {
Console::stderr($message . "\n");
} else {
echo $message . "\n";
}
} 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 \yii\console\Controller && Yii::$app->controller->isColorEnabled($stream)
|| Yii::$app instanceof \yii\console\Application && Console::streamSupportsAnsiColors($stream)) {
$message = Console::ansiFormat($message, $format);
}
return $message;
}
}
对比下 命令行应用 和 web应用,ErrorController继承的不一样,并且实现的方法也不一样的
原文地址:Yii2 捕获错误日志
标签:yii2 console error capture
智能推荐
Yii2 捕获错误日志的更多相关文章
- 一个有界任务队列的thradpoolexcutor, 直接捕获错误日志
基于官方的需要改版 1.改为有界,官方是吧所有任务添加到线程池的queue队列中,这样内存会变大,也不符合分布式的逻辑(会把中间件的所有任务一次性取完,放到本地的queue队列中,导致分布式变差) 2 ...
- 捕获JS 错误日志
最近在做项目的过程中,发现很多JS报错没有引起重视,我就想想把JS错误捕获了,然后展示在我们一个平台上 具体实现代码: window.onerror = function(message, url, ...
- 【夯实PHP基础】nginx php-fpm 输出php错误日志
本文地址 原文地址 分享提纲: 1.概述 2.解决办法(解决nginx下php-fpm不记录php错误日志) 1. 概述 nginx是一个web服务器,因此nginx的access日志只有对访问页面的 ...
- nginx php-fpm 输出php错误日志
nginx是一个web服务器,因此nginx的access日志只有对访问页面的记录,不会有php 的 error log信息. nginx把对php的请求发给php-fpm fastcgi进程来处理, ...
- 基于window.onerror事件 建立前端错误日志
QA不是万能的,用户的浏览环境非常复杂,很多情况无法靠测试用例去覆盖,所以最好建立一个前端错误日志,在真实用户端收集bug. try&catch是一个捕获前端错误的常见方法,比如: { //给 ...
- 2. SQL Server数据库状态监控 - 错误日志
原文:2. SQL Server数据库状态监控 - 错误日志 无论是操作系统 (Unix 或者Windows),还是应用程序 (Web 服务,数据库系统等等) ,通常都有自身的日志机制,以便故障时追溯 ...
- 前端监控系统(二)JS错误日志收集篇
前端监控系统 目前已经上线,欢迎使用! 服务器搭建好了,可以着手开发了. 其实前端需要分析的数据有很多,包括,PVUV, 接口请求统计,耗时统计,JS错误统计,用户使用设备统计,用户地域分布,页面用户 ...
- sql server 错误日志errorlog
一 .概述 SQL Server 将某些系统事件和用户定义事件记录到 SQL Server 错误日志和 Microsoft Windows 应用程序日志中. 这两种日志都会自动给所有记录事件加上时间戳 ...
- nginx php-fpm 输出php错误日志(转)
nginx是一个web服务器,因此nginx的access日志只有对访问页面的记录,不会有php 的 error log信息. nginx把对php的请求发给php-fpm fastcgi进程来处理, ...
随机推荐
- Kinect学习笔记(五)——更专业的深度图
这一节的内容就是把深度图转换为彩色图,然后不再使用硬解码,而是继续采用sdk里面封装好的功能来减少测量的误差,以及避免转换为灰度图时,出现绿巨人时候的掉针的bug. 下面直接 ...
- Android中Dialog
在Android中,Dialog是一个非常重要的UI, 它可以方便的给用户提示,用最简洁的方式向用户展示信息, 以下的图片是Dialog的一个整体架构,通过它,可以总体对Dialog有一个很清晰的认识 ...
- Java正则表达式, 提取双引号中间的部分
String str="this is \"Tom\" and \"Eric\", this is \"Bruce lee\", ...
- Xamarin.iOS提供没有匹配的配置文件
Xamarin.iOS提供没有匹配的配置文件 错误信息:No installed provisioning profiles match the installed ios code signing ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ1699 Best Sequence(AC自动机+状压DP)
题目,求包含所有的给定的n个DNA片段的序列的最短长度. AC自动机上的DP题. dp[S][u]表示已经包含的DNA片段集合为S,且当前后缀状态是自动机第u个结点的最短长度 dp[0][0]=0 我 ...
- POJ 3580 (伸展树)
题目链接: http://poj.org/problem?id=3580 题目大意:对一个序列进行以下六种操作.输出MIN操作的结果. 解题思路: 六个操作,完美诠释了伸展树有多么吊.注意,默认使用L ...
- css3中的几何图形shape研究
前言 估计大家在日常工作中都会用到css形状,但是目前天朝中使用到最多的估计就是圆(circle).椭圆(ellipse).各种三角形形状,但是你肯定很少看见过用几何图形或者多边图形.假如你不懂什么叫 ...
- winrt 上的翻书特效组件 源码分享 转载请说明
http://blog.csdn.net/wangrenzhu2011/article/details/10207413 (转) [TemplatePart(Name = A_PARTNAME, Ty ...
- 是否采用Sybase形式的自动字符串转义(用 '' 表示 ')
;; 关于php.ini ;; ; 这个文件必须命名为'php.ini'并放置在httpd.conf中PHPINIDir指令指定的目录中. ; 最新版本的php.ini可以在下面两个位置查看: ; h ...