Laravel Telescope
Introduction
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.
Installation
You may use the Composer package manager to install Telescope into your Laravel project:
composer require laravel/telescope
After installing Telescope, publish its assets and migrations using the telescope:install
Artisan command. After installing Telescope, you should also run the migrate
command in order to create the tables needed to store Telescope's data:
php artisan telescope:install
php artisan migrate
Finally, you may access the Telescope dashboard via the /telescope
route.
Local Only Installation
If you plan to only use Telescope to assist your local development, you may install Telescope using the --dev
flag:
composer require laravel/telescope --dev
php artisan telescope:install
php artisan migrate
After running telescope:install
, you should remove the TelescopeServiceProvider
service provider registration from your application's bootstrap/providers.php
configuration file. Instead, manually register Telescope's service providers in the register
method of your App\Providers\AppServiceProvider
class. We will ensure the current environment is local
before registering the providers:
/**
* Register any application services.
*/
public function register(): void
{
if ($this->app->environment('local')) {
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
$this->app->register(TelescopeServiceProvider::class);
}
}
Finally, you should also prevent the Telescope package from being auto-discovered by adding the following to your composer.json
file:
"extra": {
"laravel": {
"dont-discover": [
"laravel/telescope"
]
}
},
Configuration
After publishing Telescope's assets, its primary configuration file will be located at config/telescope.php
. This configuration file allows you to configure your watcher options. Each configuration option includes a description of its purpose, so be sure to thoroughly explore this file.
If desired, you may disable Telescope's data collection entirely using the enabled
configuration option:
'enabled' => env('TELESCOPE_ENABLED', true),
Data Pruning
Without pruning, the telescope_entries
table can accumulate records very quickly. To mitigate this, you should schedule the telescope:prune
Artisan command to run daily:
use Illuminate\Support\Facades\Schedule;
Schedule::command('telescope:prune')->daily();
By default, all entries older than 24 hours will be pruned. You may use the hours
option when calling the command to determine how long to retain Telescope data. For example, the following command will delete all records created over 48 hours ago:
use Illuminate\Support\Facades\Schedule;
Schedule::command('telescope:prune --hours=48')->daily();
Dashboard Authorization
The Telescope dashboard may be accessed via the /telescope
route. By default, you will only be able to access this dashboard in the local
environment. Within your app/Providers/TelescopeServiceProvider.php
file, there is an authorization gate definition. This authorization gate controls access to Telescope in non-local environments. You are free to modify this gate as needed to restrict access to your Telescope installation:
use App\Models\User;
/**
* Register the Telescope gate.
*
* This gate determines who can access Telescope in non-local environments.
*/
protected function gate(): void
{
Gate::define('viewTelescope', function (User $user) {
return in_array($user->email, [
'taylor@laravel.com',
]);
});
}
You should ensure you change your APP_ENV
environment variable to production
in your production environment. Otherwise, your Telescope installation will be publicly available.
Upgrading Telescope
When upgrading to a new major version of Telescope, it's important that you carefully review the upgrade guide.
In addition, when upgrading to any new Telescope version, you should re-publish Telescope's assets:
php artisan telescope:publish
To keep the assets up-to-date and avoid issues in future updates, you may add the vendor:publish --tag=laravel-assets
command to the post-update-cmd
scripts in your application's composer.json
file:
{
"scripts": {
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
]
}
}
Filtering
Entries
You may filter the data that is recorded by Telescope via the filter
closure that is defined in your App\Providers\TelescopeServiceProvider
class. By default, this closure records all data in the local
environment and exceptions, failed jobs, scheduled tasks, and data with monitored tags in all other environments:
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
/**
* Register any application services.
*/
public function register(): void
{
$this->hideSensitiveRequestDetails();
Telescope::filter(function (IncomingEntry $entry) {
if ($this->app->environment('local')) {
return true;
}
return $entry->isReportableException() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->isSlowQuery() ||
$entry->hasMonitoredTag();
});
}
Batches
While the filter
closure filters data for individual entries, you may use the filterBatch
method to register a closure that filters all data for a given request or console command. If the closure returns true
, all of the entries are recorded by Telescope:
use Illuminate\Support\Collection;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
/**
* Register any application services.
*/
public function register(): void
{
$this->hideSensitiveRequestDetails();
Telescope::filterBatch(function (Collection $entries) {
if ($this->app->environment('local')) {
return true;
}
return $entries->contains(function (IncomingEntry $entry) {
return $entry->isReportableException() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->isSlowQuery() ||
$entry->hasMonitoredTag();
});
});
}
Tagging
Telescope allows you to search entries by "tag". Often, tags are Eloquent model class names or authenticated user IDs which Telescope automatically adds to entries. Occasionally, you may want to attach your own custom tags to entries. To accomplish this, you may use the Telescope::tag
method. The tag
method accepts a closure which should return an array of tags. The tags returned by the closure will be merged with any tags Telescope would automatically attach to the entry. Typically, you should call the tag
method within the register
method of your App\Providers\TelescopeServiceProvider
class:
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
/**
* Register any application services.
*/
public function register(): void
{
$this->hideSensitiveRequestDetails();
Telescope::tag(function (IncomingEntry $entry) {
return $entry->type === 'request'
? ['status:'.$entry->content['response_status']]
: [];
});
}
Available Watchers
Telescope "watchers" gather application data when a request or console command is executed. You may customize the list of watchers that you would like to enable within your config/telescope.php
configuration file:
'watchers' => [
Watchers\CacheWatcher::class => true,
Watchers\CommandWatcher::class => true,
...
],
Some watchers also allow you to provide additional customization options:
'watchers' => [
Watchers\QueryWatcher::class => [
'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
'slow' => 100,
],
...
],