Hi guys, in this tutorial tips we will learn how to log all SQL Queries. There are multiple ways to create SQL Query Log and we will see three of them :

 

1- Default Log file

Log in the default log file “laravel.log” located at “storage/logs”.

To log SQL queries, we have to add the following code snippet in the “AppServiceProvider.php” file in the “boot()” function, located at “app/Providers”.

 

2- Create a custom query log file

Log SQL queries in the custom “query.log” file under the “storage/logs” directory.

 

3- Register a listener with custom query log file

Another way to do query logging is to register a listener on app/Providers/EventServiceProvider.php

Read Also  Create Custom Maintenance Page in Laravel

Next, generate Laravel events by running the command:

This command creates ‘Events’ and ‘Listeners’ directories under the ‘app’ folder. Inside ‘app/Listeners’ directory you will find the file QueryExecutedListener.php.

Open listener QueryExecutedListener and write the code as follows.

app/Listeners/QueryExecutedListener.php

I hope you have enjoyed this tutorial.

Keep learning, stay safe :).


No Comments

Leave a Reply

your email address will not be published. required fields are marked *

*
*