laravel5.4监控sql并记录在laravel.log中

来源:互联网 发布:武汉育才行知小学校长 编辑:程序博客网 时间:2024/05/16 17:23

laravel5.4监控sql并记录在laravel.log中


第一步

  • 在app\providers\eventserviceprovider.php中添加
 'Illuminate\Database\Events\QueryExecuted' => [              'App\Listeners\QueryListener'          ]  ... 

第二步

  • 新建app\Listeners\QueryListener.php
<?phpnamespace App\Listeners;use Illuminate\Support\Facades\Event;use Illuminate\Support\Facades\Log;class QueryListener{/*** Create the event listener.** @return void*/public function __construct(){//}/*** Handle the event.** @param  QueryExecuted  $event* @return void*/public function handle($event){//            $sql = str_replace("?", "'%s'", $event->sql);            $log = vsprintf($sql, $event->bindings);    Log::info($log);}} ... 

本人在laravel5.4亲测有效。。。有问题可以来提问

阅读全文
0 0