How to Log Query in Laravel

Are you looking to print the query in Laravel? Or in other words, do you want to log queries in Laravel? Both questions have slightly different answers. For printing the query, you can just print the last executed query. But I’d recommend instead of printing log the query in Laravel. By doing that, all your queries will save in the log file and you can view them anytime.

In this article, I show you both options of printing and logging queries that help to view the queries in your Laravel application.

While developing a Laravel application, sometimes you may come across a situation where you need to see if the written query is correct or not. This is because of several reasons – maybe the users are not getting an expected output, you are getting a slow response from a database server, or you don’t get any output as something is wrong with your query. In all these scenarios, if you are logging the queries then it will help to debug the issue.

Let’s see first printing queries in Laravel.

How to Print Query in Laravel

Printing a query for a current request is a one-time process. You may require to just print your query for quick fixes. In this case, Laravel logs the query in the memory. Below is a simple example to print your query in Laravel.

use Illuminate\Support\Facades\DB;
...
...

public function UserController()
{
    DB::enableQueryLog();
    $arr_user = DB::table('users')->select('name', 'email as user_email')->get();
    dd(DB::getQueryLog());
}

Now, if you run the code you will have the select query printed. Using the above technique, you can also print the queries executed through Eloquent.

The user can use this method if they don’t want to log queries. But it’s not a convenient way. Here, you need to repeat these two statements DB::enableQueryLog() and dd(DB::getQueryLog()) for every query. The better option is to store all queries in the log file.

Log Query in Laravel

Instead of printing, I personally preferred logging queries in Laravel. It is a more easy and right way for developers. And for logging, you don’t need to find each query and write the code for it. You only need to add some code in the AppServiceProvider.php and you are done.

Open the AppServiceProvider.php file, and add 2 Facades for ‘File’ and ‘DB’ as follows.

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\DB;

Next, in the boot() method write the below code that will listen to each SQL query executed and log them in the storage/logs/query.log.

public function boot()
{
    DB::listen(function($query) {
        File::append(
            storage_path('/logs/query.log'),
            $query->sql . ' [' . implode(', ', $query->bindings) . ']' . PHP_EOL
       );
    });
}

In Laravel, one more way is available to log the queries. Using this way your query logs get stored inside the storage/logs/laravel.log. For this, you need to add the below code to your routes/web.php file.

\DB::listen(function($sql) {
    \Log::info($sql->sql);
    \Log::info($sql->bindings);
    \Log::info($sql->time);
});

That’s it! Now, you can choose either one of the above methods for logging the queries in Laravel. I hope this article will be useful for your Laravel application. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

3 thoughts on “How to Log Query in Laravel

  1. Then in your App\Listeners\QueryExecutedListener do the query logging:

    public function handle(QueryExecuted $query) {
    Log::debug(__METHOD__, [‘SQL’ => $query->sql]);
    Log::debug(__METHOD__, [‘bindings’ => $query->bindings]);
    Log::debug(__METHOD__, [‘time’ => $query->time]);
    }

  2. Another way (cleaner I think) to do query logging is to register a listener on app/Providers/EventServiceProvider.php

    For example:
    ‘Illuminate\Database\Events\QueryExecuted’ => [
    ‘App\Listeners\QueryExecutedListener’
    ]

    Then in your App\Listeners\QueryExecutedListener do the query logging:

    public function handle(QueryExecuted $query) {
    Log::debug(__METHOD__, [‘SQL’ => $query->sql]);
    Log::debug(__METHOD__, [‘bindings’ => $query->bindings]);
    Log::debug(__METHOD__, [‘time’ => $query->time]);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *