Home >PHP Framework >Laravel >Using event logging SQL query to log in Laravel

Using event logging SQL query to log in Laravel

藏色散人
藏色散人forward
2019-11-15 15:17:183113browse

In this article, we will discuss how to handle query logging in Laravel. It is assumed that you are already very familiar with Laravel when reading this article. Laravel has the option to log in memory all queries run on the current request. There are some ways to do this.

Query records

If you want to save the log file in the storage/logs directory.

Needs to update: boot() function in app/Providers/AppServiceProvider.php.

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use DB;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // 新增代码
        DB::listen(function($query) {
            Log::info(
                $query->sql,
                $query->bindings,
                $query->time
            );
        });
    }
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

This way we can record the executed SQL statements, and it is also convenient for us to debug during the development process.

The above is the detailed content of Using event logging SQL query to log in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete