Thủ Thuật Yii2

  1. Log All query

If you want to log all relational queries of ActiveRecord in console application all proposed methods doesn’t help. They show only main SQL on active record’s table, \yii\debug\Moduleworks only in browser.

Alternative method to get all executed SQL queries is to log them by adding specific FileTarget to configuration:

'log' => [
    'targets' => [[
        ...
    ], [
        'class' => 'yii\log\FileTarget',
        'logFile' => '@runtime/logs/profile.log',
        'logVars' => [],
        'levels' => ['profile'],
        'categories' => ['yii\db\Command::query'],
        'prefix' => function($message) {
            return '';
        }
    ]]
]

UPDATE

In order to log insert/update/delete queries one should also add yii\db\Command::executecategory:

'categories' => ['yii\db\Command::query', 'yii\db\Command::execute']

Leave a Comment.