Thursday, October 03, 2019

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


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

EmailThis Premium lets you save unlimited bookmarks, PDF, DOCX files, PPTs and images. It also gives you a PDF copy of every page that you save. Upgrade to Premium →
Solution
~~~~~~~
add the following to App/Providers/AppServiceProvider->boot()
\DB::listen(function ($query) {    // Enclose in single quotes for string params.    $bindings = collect($query->bindings)->map(function ($param) {       if(is_numeric($param)) {         return $param;       } else {         return "'$param'";       }    });     \Log::info(\Illuminate\Support\Str::replaceArray('?', $bindings->toArray(), $query->sql)); });

~~~~~~~

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 other services as well. It's getting hard to keep a track of it all and the page load speed is fairly slow at the moment.
Where would I put \DB::enableQueryLog() and \DB::getQueryLog() to log ALL the queries and dump them? Basically I'm looking for some place in code that happens before any of the queries happen (to put enableQueryLog()) and I'm looking for a place that happens after the views render (to dump getQueryLog()).
What would be a good way to go about this?
Thanks in advance.
asked Dec 15 '16 at 11:35

DevKDevK
6,34022 gold badges99 silver badges3333 bronze badges
Here comes the perfect example:
Open app\Providers\AppServiceProvider.php and add the following to Boot() function:
DB::listen(function ($query) {     var_dump([         $query->sql,         $query->bindings,         $query->time     ]); });

17.9k1111 gold badges8585 silver badges110110 bronze badges
answered Dec 15 '16 at 11:47

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));             }         });     } }
answered Mar 31 '17 at 9:48

buzkallbuzkall
56044 silver badges1111 bronze badges
If you want to print a query which is executed on your app do following steps.
Step1: Go to your AppServiceProvider.php file. // File path App\Providers\AppServiceProvider.php
Step2: Make boot() method and paste below code.
public function boot() {         // Log queries         if (true) {             \DB::listen(function ($query) {                 \Log::info(                     $query->sql, $query->bindings, $query->time                 );             });         }     }
Step3: Now you can see you queries in lumen.log or laravel.log file. File path is laravel_app\storage\logs\laravel.log or lumen.log.
Enjoy....

1,01822 gold badges1111 silver badges2121 bronze badges
answered Sep 11 '18 at 12:00

Put this code right above the code where your query is executed
\DB::listen(function($sql) {   die(\Illuminate\Support\Str::replaceArray('?', $sql->bindings, $sql->sql)); });
Just modified for executable query:
\DB::listen(function ($query) {    // Enclose in single quotes for string params.    $bindings = collect($query->bindings)->map(function ($param) {       if(is_numeric($param)) {         return $param;       } else {         return "'$param'";       }    });     \Log::info(\Illuminate\Support\Str::replaceArray('?', $bindings->toArray(), $query->sql)); });

40355 silver badges2121 bronze badges
answered Oct 11 '18 at 9:09

ElcoElco
1111 bronze badge
add a middleware that executes after the request is done and logs your queries ... see Terminable Middlwares
answered Dec 15 '16 at 11:41

SherifSherif
1,07311 gold badge99 silver badges1818 bronze badges

Not the answer you're looking for? Browse other questions tagged php laravel laravel-5.3 or ask your own question.

Please check the attached file.
EmailThis was not able to extract useful content from the website. Hence, we have saved the webpage to a PDF file. You can find that attached along with this email.
Upgrade to Premium Plan
✔ Save unlimited bookmarks.
✔ Save PDFs, DOCX files, images and Excel sheets as email attachments.
✔ Get priority support and access to latest features.
Upgrade to Premium

No comments: