Home  >  Article  >  PHP Framework  >  How to use event recording SQL query to log in Laravel framework

How to use event recording SQL query to log in Laravel framework

不言
不言Original
2018-07-31 17:15:512825browse

In this article, we will talk about how to handle query logging in Laravel. If you're very familiar with the Laravel framework, Laravel has the option to log in memory all queries run on the current request.

Query records

If you want to save the log file in the storage/logs directory. Need 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 entire content of this article. For more laravel content, please pay attention to laravel framework introductory tutorial.

Recommended related articles:

Analysis of the architecture of applications in the Laravel framework

Real-time chat room: based on Laravel Pusher Vue Event broadcast implementation

Related course recommendations:

The latest five Laravel video tutorial recommendations in 2017

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn