1

I want to log every SQL query executed so I can debug it and then copy, paste and test it in my database tool.

I tried this solution (Yii - echo the last query) (and other similar solutions) but it is not working because I think it is for Yii1. I need it for Yii2.

I think the solution is independent of the database (I use PostgreSQL). Maybe I have to configure it in the common/config/main-local file.

1

2 Answers 2

5

If you need in Yii (1), please update your components array in main.php like this

       'components'=>array(
    #/*
                        'fixture'=>array(
                                'class'=>'system.test.CDbFixtureManager',                
                        ),
            #*/
                        'db'=>array(
                                'connectionString' => 'mysql:host=localhost;dbname=intakes_manager_test',
                                'emulatePrepare' => true,
                                'username' => 'root',
                                'password' => 'root',
                                'charset' => 'utf8',
                                'enableProfiling'=>true,
            'enableParamLogging'=>true,
                        ),
                        'log'=>array(
                                'class'=>'CLogRouter',
                                'routes'=>array(
                                        array(
                                                'class'=>'CFileLogRoute',
                                                'levels'=>'error,trace,info,warning',
                                                //'filter'=>'CLogFilter',
                    'categories'=>'system.db.*',
                    'logFile'=>'sql.log'
                                        )
                                )
                        ),
         ),

For the same in Yii 2, do like below

'components' => [
        'log' => [
            'targets' => [
                'file' => [
                    'class' => 'yii\log\FileTarget',
                ],
                'db' => [
                    'class' => 'yii\log\DbTarget',
                ],
            ],
        ],

to know more about yii 2 logging see doc here

Sign up to request clarification or add additional context in comments.

Comments

0

postgresql.conf

log_min_duration_statement = -1 # -1 is disabled, 0 logs all statements

log_min_duration_statement = 0

You would need to restart the Postgresql service

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.