Edit config/web.php
First, log must be enabled
'bootstrap' => [ 'log' ],
[file]
'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'exportInterval' => 1, ], ], ],
The default output is to runtime/logs/app.log
Note that the webserver or console user must have permission to write to the file
[database]
'log' => [ 'targets' => [ [ 'class' => 'yii\log\DbTarget', 'levels' => ['error', 'warning', 'trace'], ] ] ],
The default output is the {{%log}} table under the database corresponding to the db component
Run the following command in the yii2 root directory to generate the corresponding table schema
./yii migrate --migrationPath=@yii/log/migrations/
Note that there must also be and web under config/console.php .php with the same configuration, otherwise the command execution will not be successful.
You can also configure different log modes according to 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'], ], ], ], ],