yii2 and laravel use mongdb to print the executed statement log

This article shares the logging method of viewing mongodb statements executed on the current page under the Yii framework and the laravel framework. The main method is to configure the relevant files to achieve the purpose of debugging SQL. The specific methods are as follows:

In the yii framework, in the config/public.php file, introduce mongo:

 

 

 code show as below:

'mongodb' => array(
    'class' => 'SammayeClient',
    'uri' => 'mongodb://192.168.8.88:27017',
    'options' => [],
    'driverOptions' => [],
    'enableProfiling' => true,
    'db' => [
        'huazai' => [
            'writeConcern' => new \MongoDB\Driver\WriteConcern(1),
            'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_PRIMARY),
            'active' => true
        ]
    ],
),

Be sure to set enableProfiling to true.

Then open the mongodb log in the log:

code show as below: 

'log' => array(
    'class' => 'CLogRouter',
    'routes' => array(
        array(
            'class'=>'CFileLogRoute',//用于处理日志的类
            'levels'=>'trace, info, profile, error, warning',//标识那些级别的日志可被记录
            'filter'=>'CLogFilter', //日志过滤类
        ),

        array(
            'class' => 'CWebLogRoute',
            'levels' => 'error, warning',
            'categories' => 'system.db.*,yii\mongodb\*',
            'filter' => 'CLogFilter',
            'showInFireBug' => true, //将在firebug中显示日志
        ),
    ),
),

Then we execute the mongodb query:

 

Execute the query to see which set of mongodb is used, and the value bound to the query condition, which is convenient and intuitive to find the problem.

In this example, the OmsAttachMg collection is used to query, and the conditions are shown in the figure:

 

The above is the mongodb query print query log in yii.

Let's introduce the log of mongodb query and print query conditions in laravel:

Under the App\Providers directory, find the AppServiceProvider.php file

Add the following code in the boot() method:

ini_set('memory_limit', '512M');

$local = config('app.locale');
app('translator')->setLocale($local);
Carbon::setLocale($local);

Carbon::serializeUsing(function (Carbon $timestamp) {
    return $timestamp->format('Y-m-d H:i:s');
});

DB::listen(function($query) {
    foreach ($query->bindings as $i => $binding) {
        if ($binding instanceof \DateTime) {
            $query->bindings[$i] = $binding->format('Y-m-d H:i:s');
        } else {
            if (is_string($binding)) {
                $query->bindings[$i] = "'$binding'";
            }
        }
    }
    $tmp = str_replace(array('%', '?'), array('%%', '%s'), $query->sql);
    $tmp = vsprintf($tmp, $query->bindings);
    $tmp = str_replace("\\","",$tmp);
    Log::info($tmp."\n\n\t");

});

Then we look at the query:

 Or we directly print:

code show as below:

DB::connection('mongodb')->enableQueryLog();

$file =  DB::connection('mongodb')->collection('file')
        ->where(function($query) use ($data) {
            if(!empty($data['filekey'])) {
                $query->where('filekey', '=', trim($data['filekey']));
            }
        })
        ->first();

echo "<pre>";
print_r(DB::connection('mongodb')->getQueryLog());

So you can see the following execution log of mongodb as follows:

This tells us which set, what condition and bound value to use mongo, which is convenient for troubleshooting. 

 

 

Guess you like

Origin blog.csdn.net/lchmyhua88/article/details/111642745