欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel給生產(chǎn)環(huán)境添加監(jiān)聽事件(SQL日志監(jiān)聽)

 更新時間:2017年06月19日 10:56:03   作者:durban  
這篇文章主要給大家介紹了關(guān)于Laravel給生產(chǎn)環(huán)境添加監(jiān)聽事件(SQL日志監(jiān)聽)的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。

本文主要給大家介紹的是關(guān)于Laravel給生產(chǎn)環(huán)境添加監(jiān)聽事件(SQL日志監(jiān)聽)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:

laravel版本:5.2.*

一、創(chuàng)建監(jiān)聽器

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

or

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

會自動生成文件 app/Listeners/QueryListener.php

二、注冊事件

打開 app/Providers/EventServiceProvider.php,在 $listen 中添加 Illuminate\Database\Events\QueryExecuted 事件的監(jiān)聽器為 QueryListener

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

最終代碼如下

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);
  //
 }
}

三、添加邏輯

打開 app/Listeners/QueryListener.php

光有一個空的監(jiān)聽器是不夠的,我們需要自己實(shí)現(xiàn)如何把 $sql 記錄到日志中。為此,對 QueryListener 進(jìn)行改造,完善其 handle 方法如下:

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

最終代碼如下

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);
 }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論