Yii2写日志总结

方法一 批量文件配置写入日志:

1. 首先在config.php配置文件中配置log模块

如下:

        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',    //文件方式存储日志操作对应操作对象
                    'levels' => ['info'],
                    'categories' => ['ChinaHanGreat'],
                    'logFile' => '@app/runtime/logs/'.date("Ymd").'.log',
                    'maxLogFiles' => 20,        //最大文件数
                    'maxFileSize' => 1024 * 2,  //KB
                    'logVars' => [],
                ],
                [
                    'class' => 'yii\log\FileTarget',    //文件方式存储日志操作对应操作对象
                    'levels' => ['info', 'error', 'warning'],
                    'categories' => ['qrcode'],
                    'logFile' => '@runtime/logs/qrcode.log',
                    'maxLogFiles' => 20,        //最大文件数
                    'maxFileSize' => 1024 * 2,  //KB
                    'logVars' => [],
                ],
                'file' => [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
                'db' => [
                    //数据库存储日志对象
                    'class' => 'core\components\DbTarget',
                    'logVars' => ['_GET', '_POST'],
                    'categories' => ['erp.beforeRequest'],
                    'exportInterval' => 0,
                ]
            ],
        ],

上面配置了多个日志模块,这里要使用的是qrcode

2. 使用日志API写入日志

\Yii::warning(json_encode($sheet_details), 'qrcode');

3. 查看日志

猜你喜欢

转载自www.cnblogs.com/liuzhiqaingxyz/p/10072438.html