Laravel Telescope: First Party Laravel Package For Debugging
Last updated on
Laravel Telescope: First Party Laravel Package for Debugging
Laravel Telescope makes a wonderful companion to your local Laravel development environment. Telescope provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps, and more.
Laravel Telescope is a first party package, means it is developed by the same team who develop Laravel core and other first party packages. Laravel Telescope intercepts all of your interactions with its core API such as exceptions, log entries, database queries and much more.
Laravel Telescope provides the following features or watchers, lets briefly discussed each one of them.
Client Watcher (Incoming HTTP Requests)
Laravel\Telescope\Watchers\ClientRequestWatcher
is responsible for watching HTTP requests and it listens for the Illuminate\Http\Client\Events\ConnectionFailed
or Illuminate\Http\Client\Events\ResponseReceived
events and record the request under the Requests section.
Command Watcher
Laravel\Telescope\Watchers\CommandWatcher
is responsible for watching commands and it listens for the Illuminate\Console\Events\CommandFinished
events and record the command under the Commands section.
Following commands are ignored by default and won't be recorded by the watcher.
1/** 2 * Determine if the event should be ignored. 3 * 4 * @param mixed $event 5 * @return bool 6 */ 7private function shouldIgnore($event) 8{ 9 return in_array($event->command, array_merge($this->options['ignore'] ?? [], [10 'schedule:run',11 'schedule:finish',12 'package:discover',13 ]));14}
Schedule Watcher
Laravel\Telescope\Watchers\ScheduleWatcher
is responsible for watching scheduler and it listens for the Illuminate\Console\Events\CommandStarting
at first and then each event from the Schedule::class
and record them under the Schedule section.
1/** 2 * Record a scheduled command was executed. 3 * 4 * @param \Illuminate\Console\Events\CommandStarting $event 5 * @return void 6 */ 7public function recordCommand(CommandStarting $event) 8{ 9 if (! Telescope::isRecording() ||10 $event->command !== 'schedule:run' &&11 $event->command !== 'schedule:finish') {12 return;13 }14 15 collect(app(Schedule::class)->events())->each(function ($event) {16 $event->then(function () use ($event) {17 Telescope::recordScheduledCommand(IncomingEntry::make([18 'command' => $event instanceof CallbackEvent ? 'Closure' : $event->command,19 'description' => $event->description,20 'expression' => $event->expression,21 'timezone' => $event->timezone,22 'user' => $event->user,23 'output' => $this->getEventOutput($event),24 ]));25 });26 });27}
Batch Watcher
Laravel\Telescope\Watchers\BatchWatcher
is responsible for watching dispatched batches and it listens for the Illuminate\Bus\Events\BatchDispatched
event and record it under the Batch section.
Cache Watcher
Laravel\Telescope\Watchers\CacheWatcher
is responsible for watching cache data and it listens for the following events
-
Illuminate\Cache\Events\CacheHit
-
Illuminate\Cache\Events\CacheMissed
-
Illuminate\Cache\Events\KeyWritten
-
Illuminate\Cache\Events\KeyForgotten
1/** 2 * Register the watcher. 3 * 4 * @param \Illuminate\Contracts\Foundation\Application $app 5 * @return void 6 */ 7public function register($app) 8{ 9 $app['events']->listen(CacheHit::class, [$this, 'recordCacheHit']);10 $app['events']->listen(CacheMissed::class, [$this, 'recordCacheMissed']);11 12 $app['events']->listen(KeyWritten::class, [$this, 'recordKeyWritten']);13 $app['events']->listen(KeyForgotten::class, [$this, 'recordKeyForgotten']);14}
Dump Watcher
Laravel\Telescope\Watchers\DumpWatcher
is responsible for recording any dumps you might be using. It does listens for any events, the watcher uses the following Symfony
components and just record the dump as it is.
-
Symfony\Component\VarDumper\Cloner\VarCloner
-
Symfony\Component\VarDumper\Dumper\HtmlDumper
-
Symfony\Component\VarDumper\VarDumper
Event Watcher
Laravel\Telescope\Watchers\EventWatcher
is a global watcher and it listens for almost every event and record it under the Events section, however there are couple of events which are ignored by the Laravel Telescope and are fired internally by the Laravel framework.
1/** 2 * Determine if the event was fired internally by Laravel. 3 * 4 * @param string $eventName 5 * @return bool 6 */ 7protected function eventIsFiredByTheFramework($eventName) 8{ 9 return Str::is(10 [11 'Illuminate\*',12 'Laravel\Octane\*',13 'Laravel\Scout\Events\ModelsImported',14 'eloquent*',15 'bootstrapped*',16 'bootstrapping*',17 'creating*',18 'composing*',19 ],20 $eventName21 );22}
We will look into the further watchers in the next article.