log
通过配置Web.config来完成
1 数据库增加 ‘前缀_log’表
2 配置Web.config
'bootstrap' => ['log'], 'components' =>[ 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, //级别 'targets' => [ 'file' => [ //使用文件存储日志 'class' => 'yiilogFileTarget', 'levels' => ['error', 'warning'], ], 'legcc' =>[ //自定义模式 [例如发邮件、微信等] 'class' => 'appcomponentsLegccLogTarget', 'levels' => ['error', 'warning'], 'categories' => [ 'yiidb*', 'yiiweb*', 'yiiase*', ], 'except' => [ 'yiiwebHttpException:404', ], 'logVars' => [], ], 'db' =>[ //使用数据库存储日志 'class' => 'yiilogDbTarget', 'levels' => ['error', 'warning'], 'categories' => [ 'yiidb*', 'yiiweb*', 'yiiase*', ], 'except' => [ 'yiiwebHttpException:404', ], 'logVars' => ['_GET', '_POST', '_COOKIE', '_SESSION', '_SERVER'], ], ], ], ]
自定义模式示范
namespace appcomponents; use appmodelsLanApi; use Yii; use yiiaseInvalidConfigException; use yiilogTarget; /** * 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()); } } } } } }
邮件采用 yiiswiftmailerMailer
参考官网
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:
参考
yiiaseException;
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方法获取相关内容。