yii2 log输出到file及database
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii2 log输出到file及database相关的知识,希望对你有一定的参考价值。
编辑config/web.php
首先log要开启
‘bootstrap‘ => [ ‘log‘ ],
[file]
‘components‘ => [ ‘log‘ => [ ‘targets‘ => [ [ ‘class‘ => ‘yii\log\FileTarget‘, ‘exportInterval‘ => 1, ], ], ],
默认输出到runtime/logs/app.log
注意webserver或控制台用户要有写入该文件的权限
[database]
‘log‘ => [ ‘targets‘ => [ [ ‘class‘ => ‘yii\log\DbTarget‘, ‘levels‘ => [‘error‘, ‘warning‘, ‘trace‘], ] ] ],
默认输出db组件对应的数据库下{{%log}}表
yii2根目录下运行下面命令生成对应的table schema
./yii migrate [email protected]/log/migrations/
注意config/console.php下也要有和web.php同样的配置,否则命令执行不成功。
还可以根据不同的环境配置不同的日志模式
‘components‘ => [ ‘log‘ => [ ‘traceLevel‘ => YII_ENV == ‘dev‘ ? 3 : 0, ‘targets‘ => [ [ ‘class‘ => ‘yii\log\DbTarget‘, ‘levels‘ => YII_DEBUG ? [‘error‘, ‘warning‘, ‘trace‘] : [‘error‘], ], [ ‘class‘ => ‘yii\log\FileTarget‘, ‘levels‘ => YII_DEBUG ? [‘error‘, ‘warning‘, ‘trace‘] : [‘error‘, ‘warning‘], ], ], ], ],
以上是关于yii2 log输出到file及database的主要内容,如果未能解决你的问题,请参考以下文章
YII2数据库操作出现类似Database Exception – yiidbException SQLSTATE[HY000] [2002] No such file or director