在Laravel中执行某些查询之前禁用Query_Log()

时间:2018-06-12 04:19:25

标签: laravel-5.6 mysql-slow-query-log

我正在使用Laravel 5.6开发Web基础应用程序。有许多数据库查询要执行。

出于安全目的,我尝试将所有查询作为查询日志存储到数据库表中。我使用AppServiceProvider服务提供商来做到这一点。现在我想暂时禁用Query_Log()函数,以防止存储特定的数据库查询。 当我使用上面的代码运行app时,它在超过数据库最大执行时间时运行。

有人可以建议我这样做吗?

public function boot()
{
    if(env('App_Debug')){

        DB::listen(function($query){

           //DB::connection()->disableQueryLog();
            Query_Log::insert([
                'query_string'=>$query->sql,
                'user' => "Admin",
                'created_at' =>Carbon::now()->toDateTimeString(),
            ]);   
        });
    }
}

1 个答案:

答案 0 :(得分:0)

这就是我排除监听器的方式。我不知道是否存在功能

    DB::listen(function ($query) {
        try {
            //check if the query log is the excluded table
            if (preg_match('(query_activities)', $query->sql) == 0) {
                QueryActivity::query()->create([
                    'connection_name' => $query->connectionName,
                    'time_taken' => $query->time,
                    'query' => $query->query,
                    'bindings' => Utils::aesEncrypt(json_encode($query->bindings)),
                ]);
            }
        } catch (\Exception $exception) {

        }
    });