w3resource

Laravel (5.7) Laravel Telescope

Introduction

Laravel Telescope is a very elegant debug assistant for the Laravel framework. Telescope provides insight into the requests that are coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps and more. Telescope will make a wonderful companion to your local Laravel development environment.

Installation

Welcome: Telescope requires Laravel 5.7.7+.

You can use Composer to install Telescope into your Laravel project:

composer require "laravel/telescope":"~1.0"

Once you have installed Telescope, you should publish its assets using the telescope:install Artisan command. After you install Telescope, you need to also run the migrate command:

php artisan telescope:install
php artisan migrate

Updating Telescope

When you are updating Telescope, you need to re-publish Telescope's assets:

php artisan telescope:publish

Installing Only In Specific Environments

In the case where you plan to only use Telescope to assist your local development. You can install Telescope using the --dev flag:

composer require laravel/telescope --dev

once you run telescope:install, you need to remove the TelescopeServiceProvider service provider registration from your app configuration file. Instead, you should manually register the service provider in the register method of your AppServiceProvider:

use App\Providers\TelescopeServiceProvider;

/**
 * Registers any application services.
 *
 * @return void
 */
public function register()
{
    if ($this->app->isLocal()) {
        $this->app->register(TelescopeServiceProvider::class);
    }
}

Migration Customization

If you don't want to use Telescope's default migrations, you have to call the Telescope::ignoreMigrations method in the register method of your AppServiceProvider. You can export the default migrations using the php artisan vendor:publish --tag=telescope-migrations command.

Configuration

Once you have published Telescope's assets, its primary configuration file is located at config/telescope.php. This configuration file will allow you to configure your watcher options and each configuration option will include a description of its purpose, so ensure you thoroughly explore this file.

If desired, you can 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 quickly accumulate records. If you want to mitigate this, you need to schedule the telescope:prune Artisan command to run daily:

$schedule->command('telescope:prune')->daily();

By default, all entries older than 24 hours are pruned. You can use the hours option when calling the command to determine how long to retain Telescope data. For instance, the following command deletes all records created over 48 hours ago:

$schedule->command('telescope:prune --hours=48')->daily();

Dashboard Authorization

Telescope will expose a dashboard at /telescope. By default, you are only able to access this dashboard in the local environment. Inside your app/Providers/TelescopeServiceProvider.php file, a gate method exists. This authorization gate will control access to Telescope in non-local environments. You are totally free to modify this gate as needed to restrict access to your Telescope installation:

/**
 * Registers the Telescope gate.
 *
 * This gate will determine who can access Telescope in non-local environments.
 *
 * @return void
 */
protected function gate()
{
    Gate::define('viewTelescope', function ($user) {
        return in_array($user->email, [
            '[email protected]',
        ]);
    });
}

Filtering

Entries

You can filter the data that is recorded by Telescope via the filter callback that is registered in your TelescopeServiceProvider. By default, this callback will record all data in the local environment and exceptions, failed jobs, scheduled tasks, and data with monitored tags that are in all other environments:

/**
 * Registers any application services.
 *
 * @return void
 */
public function register()
{
    $this->hideSensitiveRequestDetails();

    Telescope::filter(function (IncomingEntry $entry) {
        if ($this->app->isLocal()) {
            return true;
        }

        return $entry->isReportableException() ||
            $entry->isFailedJob() ||
            $entry->isScheduledTask() ||
            $entry->hasMonitoredTag();
    });
}

Batches

While the filter callback will filter data for individual entries, you can use the filterBatch method to register a callback that filters all data for a given request or console command. In the case where the callback returns true, all of the entries will be recorded by Telescope:

use Illuminate\Support\Collection;

/**
 * Registers any application services.
 *
 * @return void
 */
public function register()
{
    $this->hideSensitiveRequestDetails();

    Telescope::filterBatch(function (Collection $entries) {
        if ($this->app->isLocal()) {
            return true;
        }

        return $entries->contains(function ($entry) {
            return $entry->isReportableException() ||
                $entry->isFailedJob() ||
                $entry->isScheduledTask() ||
                $entry->hasMonitoredTag();
            });
    });
}

Available Watchers

Telescope watchers will gather application data when a request or console command is executed. You can 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 will also allow you to provide additional customization options:

'watchers' => [
    Watchers\QueryWatcher::class => [
        'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
        'slow' => 100,
    ],
    ...
],

Cache Watcher

The cache watcher will record data when a cache key is hit, missed, updated and forgotten.

Command Watcher

The command watcher will record the arguments, options, exit code, and output whenever an Artisan command is executed. If you want to exclude certain commands from being recorded by the watcher, you can specify the command in the ignore option in your config/telescope.php file:

'watchers' => [
    Watchers\CommandWatcher::class => [
        'enabled' => env('TELESCOPE_COMMAND_WATCHER', true),
        'ignore' => ['key:generate'],
    ],
    ...
],

Dump Watcher

The dump watcher will record and display your variable dumps in Telescope. When using Laravel, variables can be dumped using the global dump function. The dump watcher tab has to be open in a browser for the recording to occur, otherwise the dumps are ignored by the watcher.

Event Watcher

The event watcher will record the payload, listeners, and broadcast data for any events dispatched by your application. The Laravel framework's internal events will be ignored by the Event watcher.

Exception Watcher

The exception watcher will record the data and stack trace for any reportable Exceptions that will be thrown by your application.

Gate Watcher

The gate watcher will record the data and result of gate and policy checks by your application. If you want to exclude certain abilities from being recorded by the watcher, you can specify those in the ignore_abilities option in your config/telescope.php file:

'watchers' => [
    Watchers\GateWatcher::class => [
        'enabled' => env('TELESCOPE_GATE_WATCHER', true),
        'ignore_abilities' => ['viewNova'],
    ],
    ...
],

Job Watcher

The job watcher will record the data and status of any jobs dispatched by your application.

Log Watcher

The log watcher will record the log data for any logs written by your application.

Mail Watcher

The mail watcher will allow you to view an in-browser preview of the emails along with their associated data. You can also download the email as an .eml file.

Model Watcher

The model watcher will record model changes whenever an Eloquent created, updated, restored, or deleted event is dispatched. You can specify which model events should be recorded via the watcher's events option:

'watchers' => [
    Watchers\ModelWatcher::class => [
        'enabled' => env('TELESCOPE_MODEL_WATCHER', true),
        'events' => ['eloquent.created*', 'eloquent.updated*'],
    ],
    ...
],

Notification Watcher

The notification watcher will record all notifications sent by your application. In the case where the notification triggers an email and you have enabled the mail watcher, the email is also available for preview on the mail watcher screen.

Query Watcher

The query watcher will record the raw SQL, bindings, and execution time for all the queries that are executed by your application. The watcher will also tag any queries slower than 100ms as slow. You can customize the slow query threshold using the watcher's slow option:

'watchers' => [
    Watchers\QueryWatcher::class => [
        'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
        'slow' => 50,
    ],
    ...
],

Redis Watcher

Warning: Redis events has to be enabled for the Redis watcher to function. You can enable Redis events by calling Redis::enableEvents() in the boot method of your app/Providers/AppServiceProvider.php file.

The Redis watcher will record all Redis commands executed by your application. In the case where you are using Redis for caching, cache commands are also recorded by the Redis Watcher.

Request Watcher

The request watcher will record the request, headers, session, and response data associated with any requests handled by the application. You can limit your response data via the size_limit (in KB) option:

'watchers' => [
    Watchers\RequestWatcher::class => [
        'enabled' => env('TELESCOPE_REQUEST_WATCHER', true),
        'size_limit' => env('TELESCOPE_RESPONSE_SIZE_LIMIT', 64),
    ],
    ...
],```

Schedule Watcher

The schedule watcher will record the command and output of any of the scheduled tasks run by your application.



Follow us on Facebook and Twitter for latest update.