关于yii2 的db log 日志 错误处理errorHandler

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'],
                ],
            ],
        ],
]
View Code

 

自定义模式示范
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());
                    }
                }
            }
        }
    }
}
View Code

 邮件采用  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方法获取相关内容。

posted @ 2017-04-06 17:36  Dzs  阅读(801)  评论(0编辑  收藏  举报