Laravel 8 Store Log Of Eloquent SQL Queries

Reading Time: 5 minutes
2,425 Views

Inside this article we will see the concept 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.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


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.

We hope this article helped you to learn Laravel 8 Store Log Of Eloquent SQL Queries Tutorial 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