码迷,mamicode.com
首页 > 其他好文 > 详细

我 && yii2(日志埋点,邮件提醒)

时间:2016-11-02 18:10:30      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:target   data-   err   test   布局   swift   .com   charset   php   

今天试着把yii2 的日志,如果发送邮件的形式实现,具体实现如下

1.环境介绍

lnmp php5.6, mysql5.5, lnmp1.2

yii2-advanced

2.配置文件的编写

  • 在frontend/config/main.php 添加mailer 和 log 的配置
‘mailer‘    => require_once ‘../config/mail.php‘,
‘log‘   => require_once ‘../config/log.php‘,

  

  • mailer的配置如下(frontend/config/mail.php)
<?php
return [
    ‘class‘ => ‘yii\swiftmailer\Mailer‘,
    ‘viewPath‘ => ‘@common/mail‘,
    ‘useFileTransport‘ => false,
    ‘transport‘ => [
        ‘class‘ => ‘Swift_SmtpTransport‘,
        ‘host‘ => ‘smtp.163.com‘,
        ‘username‘ => ‘**@163.com‘,
        ‘password‘ => ‘****‘,
        ‘port‘ => ‘25‘,
        ‘encryption‘ => ‘tls‘,
    ],
    ‘messageConfig‘=>[
        ‘charset‘=>‘UTF-8‘,
        ‘from‘=>[‘zeopean@163.com‘=>‘zeopean‘]
    ],
];

  

  • log的配置如下(frontend/config/log.php)
<?php
return [
    ‘traceLevel‘ => YII_DEBUG ? 3 : 0,
    ‘targets‘ => [
        [
            ‘class‘ => ‘yii\log\EmailTarget‘,
            ‘levels‘ => [‘error‘,‘info‘],
            ‘categories‘ => [‘email_log‘],      #该email_log 会在日志方法使用时使用到
            ‘message‘ => [
                ‘from‘ => [‘**@163.com‘],
                ‘to‘ => [‘**@qq.com‘],
                ‘subject‘ => ‘Database errors at example.com‘,
            ],
        ],
        [
            ‘class‘ => ‘yii\log\FileTarget‘,
            ‘levels‘ => [‘error‘, ‘warning‘],
        ],
    ],
];

  

3.路由编写

#(frontend/config/routes.php)

return [‘GET log-test‘  => ‘demo/log-test‘];

  

4.控制器编写

<?php
namespace frontend\controllers;

use Yii;
use yii\debug\models\search\Log;
use yii\log\EmailTarget;
use yii\web\Controller;


/**
 * Site controller
 */
class DemoController extends Controller{

    public $layout = false; //不使用布局
    public  $enableCsrfValidation=false;
    
    /**
     * ======================================================================
     * 测试日志生成 - 发送邮件
     */
    public function actionLogTest()
    {
        Yii::info("logging info", ‘email_log‘);
    }
}

  

ok,运行后,邮件便可以收到了。至于其他的日志模式,就需要按照实际的业务来进行了。

我 && yii2(日志埋点,邮件提醒)

标签:target   data-   err   test   布局   swift   .com   charset   php   

原文地址:http://www.cnblogs.com/zeopean/p/6023615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!