How to Store Log Of Eloquent SQL Queries in Laravel 9?

Apr 15, 2022 . Admin

Hi Dev,

I am going to explain you example of laravel 9 store log of eloquent sql queries into application log file as well as into a custom log file. Managing queries logs help to debug, find the details of running queries in application.

Log files are those files in which application status like errors, information, warnings, etc stored. Log files help application developer to debug applications.

Laravel by default provide /storage/logs/laravel.log file location where it stores application logs. But sometime we may need to create log file with specific task. Also for maintaining logs we have a composer package which exactly do the same thing what we are discussing. Click here to learn about LogViewer package and it’s working.

Let’s get started.

Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Concept 1: Store In Default Log File

Laravel default log file location is storage/logs/laravel.log. We are going to store SQL log in the file. But before to store logs we need to do some configuration.

Open AppServiceProvider.php file from app/Providers folder.

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function ($query) {
            Log::info(
                $query->sql,
                $query->bindings,
                $query->time
            );
        });
    }
}

We have added query log logic into boot() method.

Whenever we run any queries by means of using Models, Raw queries, Eloquent, etc will be then automatically queries will be managed into laravel.log file.

Concept 2: Create a Custom Log File

Sometime we may need to create log file with specific task. For example, if someone works with payment task and need all logs at a fixed place, so this concept will help you.

We can create a custom log file to store log data.

Let’s create query.log file in the storage/logs folder. In the boot() method of AppServiceProvider.php file, add this following code –

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\DB;
use File;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function($query) {
            File::append(
                storage_path('/logs/query.log'),
                '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . $query->sql . ' [' . implode(', ', $query->bindings) . ']' . PHP_EOL . PHP_EOL
            );
        });
    }
}

We have added queries log into a custom log file concept into boot() method.

Whenever we run any queries by means of using Models, Raw queries, Eloquent, etc will be then automatically queries will be managed into query.log file. This file will be in /storage/logs folder.

I hope it can help you...

#Laravel 9