Commit 54e6d6c5 by 杨树贤

调试代码

parent 169e1219
Showing with 32 additions and 0 deletions
...@@ -3,7 +3,10 @@ ...@@ -3,7 +3,10 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
...@@ -28,6 +31,35 @@ class EventServiceProvider extends ServiceProvider ...@@ -28,6 +31,35 @@ class EventServiceProvider extends ServiceProvider
{ {
parent::boot($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日志文件中
//
// });
// //
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment