Laravel 8 Store Log Of Eloquent SQL Queries

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. to learn about LogViewer package and it’s working.

Learn More -

Let’s get started.

Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command -

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel -

$ php artisan serve

This command outputs -

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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.

Originally published at https://onlinewebtutorblog.com on December 10, 2021.

--

--

--

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Add OpenApi (Swagger) for Documenting a Spring REST API

How to Add Swagger to Spring Boot

C# or Java, Which is The Best Combination to Learn Selenium Automation Testing?

Testing From Trenches, Update My Credit Card Number — Tentamen Software Testing Blog

Meetup And Learn, Testing Stories From The Aviation

Google Summer of Code 2021 with Oppia

How Programming Helps Me Heal

Saving money on your next software project

Amazon DynamoDB — Provisioning Write Capacity for Global Secondary Indexes (GSIs)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Online Web Tutor

Online Web Tutor

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

More from Medium

How To Get File Size From URL in Laravel 8 Tutorial

Laravel Advance | Laravel Broadcast Redis Socket io Tutorial

Changing the routing namespace in Laravel 8 like Laravel 7

How to Override Auth Register Method in Laravel 8