Yii2log output to file and database edit config/web. php
First, log must be enabled.
'bootstrap' => [ 'log'],
[File]
'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'exportInterval' => 1, ], ], ],
Default output to runtime/logs/app. log
Note that webserver or console users must have the permission to write the file
[Database]
'log' => [ 'targets' => [ [ 'class' => 'yii\log\DbTarget', 'levels' => ['error', 'warning', 'trace'], ] ]],
The {% log} table of the database corresponding to the db component is output by default.
Run the following command in the yii2 directory to generate the corresponding table schema
./yii migrate --migrationPath=@yii/log/migrations/
Note that the configuration in config/console. php must be the same as that in web. php. Otherwise, the command cannot be executed successfully.
You can also configure different log modes based on different environments.
'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'], ], ], ],],