<?php namespace App\Providers; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'App\Events\SomeEvent' => [ 'App\Listeners\EventListener', ], ]; /** * Register any other events for your application. * * @param \Illuminate\Contracts\Events\Dispatcher $events * @return void */ public function boot(DispatcherContract $events) { parent::boot($events); // //监听laravel框架执行的sql语句 并记录到log日志文件中 // DB::listen(function(QueryExecuted $event){ // // $sql = $event->sql; //执行的sql语句 // $bindings = $event->bindings; //sql语句对应绑定的sql值 // $time = $event->time; //sql执行的时间 // // $bindings = array_map(function($binding){ // if(is_string($binding)) // { // return "'$binding'"; //如果$binding是字符串类型 则要将$binding的左右两边拼接上'' 即 字符串=》 '字符串' // } // else if($binding instanceof \DateTime) // { // return $binding->format("'Y-m-d H:i:s'"); //如果$binding是一个DateTime对象 则格式化时间 并在左右俩边拼接上'' // } // else // { // return $binding; //直接返回$binding原本的值 // } // }, $bindings); // // $sql = str_replace('?', '%s', $sql); //将sql语句中的?换成%s //// $sql = sprintf($sql, ...$bindings); //将$bindings数组中的每一个元素替换掉$sql中的%s的位置 // // Log::info("sql_log", ["sql" => $sql, "execute_time" => $time]); //将最终生成的sql语句写入到log日志文件中 // // }); // } }