Skip to content

Laravel 5.5 – How to log all Eloquent Queries

Last updated on November 23, 2022

In this post, I will show you a simple method to log each and every Eloquent Query your application executes. We gonna log queries to the storage/logs/laravel.log file.

To log database queries we gonna add a database lister as shown in the below example. Open your app/Providers/AppServiceProvider.php and add the following code to boot() method:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use DB;
use Log;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Schema::defaultStringLength(191);

        DB::listen(function($query) {
            Log::info(
                $query->sql,
                $query->bindings,
                $query->time
            );
        });
    }

}

Example log data

[2017-12-04 18:57:33] local.INFO: select * from `tasks`  
[2017-12-04 18:57:33] local.INFO: select count(*) as aggregate from `tasks`  
[2017-12-04 18:57:33] local.INFO: select * from `tasks` limit 15 offset 0  
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments