AppServiceProvider.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. class AppServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * Register any application services.
  10. *
  11. * @return void
  12. */
  13. public function register()
  14. {
  15. //
  16. }
  17. /**
  18. * Bootstrap any application services.
  19. *
  20. * @return void
  21. */
  22. public function boot()
  23. {
  24. // FIXME LILIN 开发环境打印SQL
  25. if (getenv('APP_ENV') == 'local_lilin'){
  26. DB::listen(function ($query) {
  27. $tmp = str_replace('?', '"' . '%s' . '"', $query->sql);
  28. $qBindings = [];
  29. foreach ($query->bindings as $key => $value) {
  30. if (is_numeric($key)) {
  31. $qBindings[] = $value;
  32. } else {
  33. $tmp = str_replace(':' . $key, '"' . $value . '"', $tmp);
  34. }
  35. }
  36. $tmp = vsprintf($tmp, $qBindings);
  37. $tmp = str_replace("\\", "", $tmp);
  38. Log::info(' execution time: ' . $query->time . 'ms; ' . $tmp);
  39. });
  40. }
  41. }
  42. }