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


Share:
 | Published By: Admin | Published On: Jan 21, 2022 | Category: Laravel 8

Hi Dev,

I am going to explain you example of laravel 8 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.

Step : 1 - Installation of Laravel Application

Use this command then download laravel project setup :

composer create-project --prefer-dist laravel/laravel blog
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...

Share: