🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 监听laravel 执行的mysql语句并且写入文件中 1. 找到`app/Providers/AppServiceProvider.php`文件 2. 在boot方法中加入这个方法 ~~~ public function boot() { $this->listenSql(); } ~~~ `listenSql`方法内容如下 ~~~ \DB::listen( function ($sql) { foreach ($sql->bindings as $i => $binding) { if ($binding instanceof \DateTime) { $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\''); } else { if (is_string($binding)) { $sql->bindings[$i] = "'$binding'"; } } } // Insert bindings into query $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql); $query = vsprintf($query, $sql->bindings); // Save the query to file $logFile = fopen( storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'), 'a+' ); fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL); fclose($logFile); } ); ~~~ 最终效果 ![](https://img.kancloud.cn/29/78/2978dccb02ac554040714acd281a66a2_320x123.png) 内容 ~~~ 2022-07-11 14:10:33: select * from `user where `status` = 0 and `expire_at` <= '2022-07-11 00:00:00' 2022-07-11 14:10:54: select * from `user ` where `status` = 0 and `expire_at` <= '2022-07-11 00:00:00' ~~~