编辑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 --migrationPath=@yii/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'], ], ], ], ],
Atas ialah kandungan terperinci yii2中关于log输出到file及database的简单实例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!