Laravel 5.3 - How to log all queries on a page?

后端 未结 7 1574
生来不讨喜
生来不讨喜 2020-12-24 12:48

My team and I are working on a rather big project. There\'s queries going on everywhere - in controllers, in view composers in views (lazy loading) and probably in some othe

7条回答
  •  星月不相逢
    2020-12-24 13:34

    You can add this to the Providers/AppServiceProvider.php file and check them in the laravel log file with tail:

    tail -f storage/logs/laravel.log
    

    You can even filter with queries you want to log. For example, here I was using Laravel Passport, and didn't want to log all the oauth queries.

    use Illuminate\Support\Facades\App;
    use Illuminate\Support\Facades\Event;
    use Illuminate\Support\Facades\Log;
    
    public function register() {
        if (App::environment('local') && env('APP_URL') == 'http://localhost') {
            Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
                // filter oauth ones
                if (!str_contains($query->sql, 'oauth')) {
                    Log::debug($query->sql . ' - ' . serialize($query->bindings));
                }
            });
        }
    }
    

提交回复
热议问题