log

通过配置Web.config来完成

1 数据库增加 ‘前缀_log’表

2 配置Web.config

'bootstrap' => ['log'],
'components' =>[
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0, //级别
'targets' => [
'file' => [ //使用文件存储日志
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
'legcc' =>[ //自定义模式 [例如发邮件、微信等]
'class' => 'app\components\LegccLogTarget',
'levels' => ['error', 'warning'],
'categories' => [
'yii\db\*',
'yii\web\*',
'yii\base\*',
],
'except' => [
'yii\web\HttpException:404',
],
'logVars' => [],
],
'db' =>[ //使用数据库存储日志
'class' => 'yii\log\DbTarget',
'levels' => ['error', 'warning'],
'categories' => [
'yii\db\*',
'yii\web\*',
'yii\base\*',
],
'except' => [
'yii\web\HttpException:404',
],
'logVars' => ['_GET', '_POST', '_COOKIE', '_SESSION', '_SERVER'],
],
],
],
]
自定义模式示范
namespace app\components;
use app\models\LanApi;
use Yii;
use yii\base\InvalidConfigException;
use yii\log\Target; /**
* DbTarget stores log messages in a database table.
*
*/
class LegccLogTarget extends Target
{
const CACHE_SENDTIME = 'legccLogTargetTime'; /**
* Initializes the DbTarget component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[db]] is invalid.
*/
public function init()
{
parent::init();
} /**
* Stores log messages to .Weixin
*/
public function export()
{
$cache = Yii::$app->cache;
$lastSendTime = intval($cache->get(self::CACHE_SENDTIME));
if((time() - $lastSendTime) < 100) {
return false;
}
$emailArray = ['']; foreach ($this->messages as $message) {
list($text, $level, $category, $timestamp) = $message;
if (!is_string($text)) {
if ($text instanceof \Throwable || $text instanceof \Exception){ $params =[$emailArray, '程序异常 '.date('Y-m-d H:i:s', $timestamp), $text->getMessage().'<br/>'.$text->getFile() . ' ' . $text->getLine()];
$result = MailQueue.addToQueue($params);//发送邮件,返回结果
if($result['code'] == 0) {
$cache->set(self::CACHE_SENDTIME, time());
}
}
}
}
}
}

邮件采用  yii\swiftmailer\Mailer

参考官网
http://www.yiiframework.com/doc-2.0/yii-log-logger.html
http://www.yiifans.com/yii2/guide/runtime-logging.html
http://www.yiiframework.com/doc-2.0/yii-log-target.html
http://www.cnblogs.com/yhdsir/p/5896820.html

备注 :yii migrate --migrationPath=@yii/log/migrations/ 需要进行数据库表的迁移

errorHandler:
参考
yii\base\Exception;
http://blog.csdn.net/dasgk/article/details/52180696

如果在web/index.php中defined('YII_DEBUG') or define('YII_DEBUG', true);
可以采用 可以在web.php中对excepiton的属性exceptionView重新定义 eg:
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
'exceptionView'=>'@app/views/site/error_exceptionView.php'
],
]
如果 设置为false ,直接调转到site/error的页面,在该页面可以通过excepiton的get方法获取相关内容。

最新文章

  1. 自定义shape文件
  2. Homebrew- MAC上的包管理利器
  3. 数理方程:Fourier级数
  4. cocos2d-x 在mac下执行 demo
  5. STL string常用操作指令
  6. Qt MinGW 使用联合编译IncrediBuild
  7. web.config中httpModules和Modules的区别
  8. svn代码管理的使用工作流程
  9. 新旧apache HttpClient 获取httpClient方法
  10. JAVA基础4——谈谈HashCode与HashMap相关概念
  11. linux 学习笔记 软件包管理
  12. git 错误汇总
  13. 常用curl测试命令
  14. linux 和 主机通信的另类方法
  15. Idea导出可运行Jar包
  16. html固定宽度下拉框内容显示不全问题解决方法
  17. mysql数据库数据监测
  18. Ubuntu12.04下apache服务器的安装也配置
  19. Nmap的活跃主机探测常见方法
  20. vmware esxi 4.1单IP访问不同机器

热门文章

  1. Java-JNA使用心得
  2. python的高阶函数与匿名函数
  3. Android字体大小怎么自适应不同分辨率?
  4. nodejs 操作mongodb, 增删改查
  5. 剑指Offer - 九度1348 - 数组中的逆序对
  6. 《Cracking the Coding Interview》——第14章:Java——题目4
  7. 【Kth Smallest Element in a BST 】cpp
  8. 测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低)
  9. python 学习分享-socketserver
  10. heat应用