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

Using event logging SQL query to log in Laravel

藏色散人
Release: 2019-11-15 15:17:18
forward
3175 people have browsed it

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()
    {
        //
    }
}
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template