Laravel 10 How To Store Log Of Eloquent SQL Queries

Reading Time: 5 minutes
358 Views

Logging Eloquent SQL queries in Laravel 10 may be highly useful for debugging, speed optimisation, and learning how your application interacts with the database. You may analyse data manipulation activities and identify potential bottlenecks in your database interactions by storing a log of executed SQL queries.

This tutorial will walk you through the steps of saving the log of Eloquent SQL queries in Laravel 10. We’ll look at Laravel’s query logging functionality and show you how to use it to collect and save executed SQL queries in your selected storage location.

Read More: Roblox Unblocked: The Ultimate Gaming Platform for Everyone

Log files stores the information of application activity like of errors, warnings, notices, info, etc. 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. Additionally, for maintaining logs we have a composer package which exactly do the same thing what we are discussing.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Method #1: Store Logs In Default Laravel 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.
     */
    public function register(): void
    {
        //
    }

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

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

Read More: Laravel Send Push Notification to IOS Using Firebase

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

Method #2: Custom Log File for Laravel Query Logs

Sometime you may need to create a custom 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.

Let’s create a 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.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        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 custom log file.

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

Read More: Laravel 10 Send Push Notification to Android Using Firebase

We hope this article helped you to learn Laravel 10 How To Store Log Of Eloquent SQL Queries in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more