realtebo
realtebo

Reputation: 25681

Yii2: is it possible to filter out some query from being logged into Yii Debugger?

I love Yii2 log system, but I'd like to 'narrow' a bit the log avoiding logging of query like

SHOW CREATE TABLE `tbl_product`

or RBAC-related like

SELECT * FROM `tbl_auth_assignment` WHERE `user_id`='1'

Where and how can I customize/configure the logging system to exclude queries by sql text (not by log category/level as usually)?

I know I can extend FileTarget to avoid some messages from being written on disk. For example:

<?php

namespace common\components;

class CustomFileTarget extends \yii\log\FileTarget
{   
    // this is an override
    public function collect($messages, $final)
    {

        foreach ($messages as $index  => $message) {
            // See http://www.yiiframework.com/doc-2.0/yii-log-logger.html#$messages-detail
            if ($message[2] == 'yii\\db\\Command::query') {

                $is_full_cols       = (stripos($message[0], "SHOW FULL COLUMNS")===0);
                $is_create_table    = (stripos($message[0], "SHOW CREATE TABLE")===0);

                if ($is_full_cols OR $is_create_table)  {
                    unset($messages[$index]);
                }
            } 


        }
        parent::collect($messages, $final);
    }
}

then configure my app to use it

'log'       => [

        'targets'   => [
            ....
            [
                'class'         => 'common\components\CustomFileTarget',
                'categories'    => ['*'],
            ],
        ],

But I'd like to AVOID these message be found in the Yii Debugger's database panel. Or, better, to complete remove logging of these SQLs at all. (Probably this need to extend active query base class)

Doing more lookup into the Yii2 code, I found where ALL of SQLs were logged: it's in the file vendor\yiisoft\yii2\db\Command.php in the function queryInternal. I'm thinking that I should override this class to override this function, and override ActiveQuery and ActiveRecord classes to force these to use my overridden Command class...

Upvotes: 2

Views: 568

Answers (1)

Simon Tsang
Simon Tsang

Reputation: 68

For me I found that there is log component set the in the config/web.php which you could extend the yii\log\FileTarget to customise what is being log.

There is also the Yii Debugger which I think is what you are referring to. I'm not sure how this can be customise but you can take a look at : cache site

Upvotes: 1

Related Questions