diff --git a/src/app/Listeners/SqlDebug.php b/src/app/Listeners/SqlDebug.php new file mode 100644 --- /dev/null +++ b/src/app/Listeners/SqlDebug.php @@ -0,0 +1,81 @@ + + */ + public function subscribe(Dispatcher $events): array + { + if (!\config('app.debug')) { + return []; + } + + return [ + QueryExecuted::class => 'handle', + TransactionBeginning::class => 'handle', + TransactionCommitted::class => 'handle', + TransactionRolledBack::class => 'handle' + ]; + } + + /** + * Handle the event. + * + * @param object $event An event object + */ + public function handle(object $event): void + { + switch(get_class($event)) { + case TransactionBeginning::class: + $query = 'begin'; + break; + case TransactionCommitted::class: + $query = 'commit'; + break; + case TransactionRolledBack::class: + $query = 'rollback'; + break; + default: + $query = sprintf( + '%s [%s]: %.4f sec.', + $event->sql, + self::serializeSQLBindings($event->bindings, $event->sql), + $event->time / 1000 + ); + } + + \Log::debug("[SQL] {$query}"); + } + + /** + * Serialize a bindings array to a string. + */ + private static function serializeSQLBindings(array $array, string $sql): string + { + $ipv = preg_match('/ip([46])nets/', $sql, $m) ? $m[1] : null; + + $serialized = array_map(function ($entry) use ($ipv) { + if ($entry instanceof \DateTime) { + return $entry->format('Y-m-d h:i:s'); + } elseif ($ipv && is_string($entry) && strlen($entry) == ($ipv == 6 ? 16 : 4)) { + // binary IP address? use HEX representation + return '0x' . bin2hex($entry); + } + + return $entry; + }, $array); + + return implode(', ', $serialized); + } +} diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php --- a/src/app/Providers/AppServiceProvider.php +++ b/src/app/Providers/AppServiceProvider.php @@ -4,7 +4,6 @@ use Illuminate\Database\Query\Builder; use Illuminate\Support\Facades\Blade; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; use Laravel\Passport\Passport; @@ -20,27 +19,6 @@ Passport::ignoreRoutes(); } - /** - * Serialize a bindings array to a string. - */ - private static function serializeSQLBindings(array $array, string $sql): string - { - $ipv = preg_match('/ip([46])nets/', $sql, $m) ? $m[1] : null; - - $serialized = array_map(function ($entry) use ($ipv) { - if ($entry instanceof \DateTime) { - return $entry->format('Y-m-d h:i:s'); - } elseif ($ipv && is_string($entry) && strlen($entry) == ($ipv == 6 ? 16 : 4)) { - // binary IP address? use HEX representation - return '0x' . bin2hex($entry); - } - - return $entry; - }, $array); - - return implode(', ', $serialized); - } - /** * Load the override config and apply it * @@ -90,20 +68,6 @@ Schema::defaultStringLength(191); - // Log SQL queries in debug mode - if (\config('app.debug')) { - DB::listen(function ($query) { - \Log::debug( - sprintf( - '[SQL] %s [%s]: %.4f sec.', - $query->sql, - self::serializeSQLBindings($query->bindings, $query->sql), - $query->time / 1000 - ) - ); - }); - } - // Register some template helpers Blade::directive( 'theme_asset', diff --git a/src/app/Providers/EventServiceProvider.php b/src/app/Providers/EventServiceProvider.php --- a/src/app/Providers/EventServiceProvider.php +++ b/src/app/Providers/EventServiceProvider.php @@ -2,9 +2,6 @@ namespace App\Providers; -use Illuminate\Support\Facades\Event; -use Illuminate\Auth\Events\Registered; -use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider @@ -15,9 +12,15 @@ * @var array> */ protected $listen = [ - Registered::class => [ - SendEmailVerificationNotification::class, - ], + ]; + + /** + * The subscriber classes to register. + * + * @var array + */ + protected $subscribe = [ + \App\Listeners\SqlDebug::class, ]; /**