Edit config/web.php
First the log must be enabled
'bootstrap' => [ 'log' ],
[file]
##
'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'exportInterval' => 1, ], ], ],
'log' => [ 'targets' => [ [ 'class' => 'yii\log\DbTarget', 'levels' => ['error', 'warning', 'trace'], ] ] ],
./yii migrate --migrationPath=@yii/log/migrations/
'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'], ], ], ], ],
The above is the detailed content of A simple example of log output to file and database in yii2. For more information, please follow other related articles on the PHP Chinese website!