Laravel 10 Store Log Of Eloquent SQL Queries

26-Apr-2023

.

Admin

Laravel 10 Store Log Of Eloquent SQL Queries

Hi Friends,

I am going to explain to you an example of Laravel 10 store log of eloquent SQL queries into an application log file as well as into a custom log file. Managing query logs help to debug, and find the details of running queries in the 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 provides /storage/logs/laravel.log file location where it stores application logs. But sometimes we may need to create a log file with specific tasks. Also for maintaining logs, we have a composer package that exactly does the same thing that we are discussing. Click here to learn about the 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 the 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 the SQL log in the file. But before storing logs we need to do some configuration.

Open the AppServiceProvider.php file from the app/Providers folder.

app/Providers/AppServiceProvider.php

<?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 the 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: Add Custom Log File

Sometimes we may need to create a log file with specific tasks. For example, if someone works with a payment task and needs all logs in a fixed place, 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 –

app/Providers/AppServiceProvider.php

<?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 the 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 the query.log file. This file will be in /storage/logs folder.

I hope it can help you...

#Laravel 10