Home > Backend Development > PHP Tutorial > Laravel adds listening event code sharing to production environment

Laravel adds listening event code sharing to production environment

小云云
Release: 2023-03-20 10:14:01
Original
1363 people have browsed it

本文主要和大家介绍了关于Laravel给生产环境添加监听事件(SQL日志监听)的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,希望能帮助到大家。

laravel版本:5.2.*

一、创建监听器

php artisan make:listener QueryListener --event=Illuminate\\Database\\Events\\QueryExecuted
Copy after login

or

sudo /usr/local/bin/php artisan make:listener QueryListener --event=Illuminate\\Database\\Events\\QueryExecuted
Copy after login

会自动生成文件 app/Listeners/QueryListener.php

二、注册事件

打开 app/Providers/EventServiceProvider.php,在 $listen 中添加 Illuminate\Database\Events\QueryExecuted 事件的监听器为 QueryListener

protected $listen = [ 
 'Illuminate\Database\Events\QueryExecuted' => [
  'App\Listeners\QueryListener',
 ],
];
Copy after login

最终代码如下

namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
 /**
  * The event listener mappings for the application.
  *
  * @var array
  */
 protected $listen = [
  'App\Events\SomeEvent' => [
   'App\Listeners\EventListener',
  ],
  'Illuminate\Database\Events\QueryExecuted' => [
   'App\Listeners\QueryListener',
  ],
 ];
 /**
  * Register any other events for your application.
  *
  * @param \Illuminate\Contracts\Events\Dispatcher $events
  * @return void
  */
 public function boot(DispatcherContract $events)
 {
  parent::boot($events);
  //
 }
}
Copy after login

三、添加逻辑

打开 app/Listeners/QueryListener.php

光有一个空的监听器是不够的,我们需要自己实现如何把 $sql 记录到日志中。为此,对 QueryListener 进行改造,完善其 handle 方法如下:

$sql = str_replace("?", "'%s'", $event->sql);
$log = vsprintf($sql, $event->bindings);
Log::info($log);
Copy after login

最终代码如下

namespace App\Listeners;
use Log;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class QueryListener
{
 /**
  * Create the event listener.
  *
  * @return void
  */
 public function __construct()
 {
  //
 }
 /**
  * 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::info($log);
 }
}
Copy after login

相关推荐:

微信小程序页面间跳转如何监听事件

自定义监听事件的代码示例

有关微信小程序页面间的跳转如何监听事件详解

The above is the detailed content of Laravel adds listening event code sharing to production environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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