Home >Backend Development >PHP Tutorial >How to use the SQL logging function implemented by the Laravel framework

How to use the SQL logging function implemented by the Laravel framework

不言
不言Original
2018-06-19 16:33:531953browse

This article mainly introduces the SQL logging function implemented by the Laravel framework. It summarizes and analyzes the Laravel framework's monitoring and recording SQL-related operating techniques and precautions in the form of examples. Friends in need can refer to this article

The example describes the SQL logging function implemented by the Laravel framework. Share it with everyone for your reference, the details are as follows:

During the project development process or performance optimization, it is often necessary to check the execution of sql. However, the Laravel log does not record the execution of sql by default. Fortunately, there are relevant interfaces, and we can easily use the SQl logging function.

Add the following to $listen in App\Providers\EventServiceProvider:class

protected $listen = [
  'App\Events\Event' => [
    'App\Listeners\EventListener',
  ],
  // 新增SqlListener监听QueryExecuted
  'Illuminate\Database\Events\QueryExecuted' => [
    'App\Listeners\SqlListener',
  ],
];

New SqlListener listener Device

Method 1, manually created, in the App\Listeners\SqlListener.php file, the content is as follows

namespace App\Listeners;
use Illuminate\Database\Events\QueryExecuted;
class SqlListener {
  /**
   * Create the event listener.
   *
   * @return void
   */
  public function __construct() {
    //
  }
  /**
   * Handle the event.
   *
   * @param =QueryExecuted $event
   * @return void
   */
  public function handle(QueryExecuted $event) {
    // 在这里编写业务逻辑
  }
}

Method 2, use the command line to create, the command is as follows

// 该命令必须在项目跟目录下执行,因为项目跟目录下才有artisan文件。
// 该命令可以自动创建SqlListener文件,但是QueryExecuted这个类的导入可能会有点问题,自己改下。

> php artisan make:listener SqlListener -e=QueryExecuted

Write the business of recording sql in the handle method Logic, such as:

/**
 * Handle the event.
 *
 * @param =QueryExecuted $event
 * @return void
 */
public function handle(QueryExecuted $event) {
  $sql = str_replace("?", "'%s'", $event->sql);
  $log = vsprintf($sql, $event->bindings);
  $log = '[' . date('Y-m-d H:i:s') . '] ' . $log . "\r\n";
  $filepath = storage_path('logs\sql.log');
  file_put_contents($filepath, $log, FILE_APPEND);
  // 这里也可以直接用Log::info() 里的函数,只是这样会和其他调试信息掺在一起。
  // 如果要用Log里的函数,别忘记了引入Log类。
}

The above is the entire content of this article, I hope it will be helpful to everyone’s study, more Please pay attention to the PHP Chinese website for related content!

Related recommendations:

For the functions of Laravel framework template loading and assigning variables and simple routing

How to solve the problem in Laravel The problem that the log cannot be written

The above is the detailed content of How to use the SQL logging function implemented by the 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