Laravel Debugging Part 1 - Laravel Telescope

Laravel Debugging Part 1 - Laravel Telescope

Learn how to use Laravel Telescope for debugging and monitoring your Laravel application.

Introduction

Laravel Telescope is a powerful debugging and monitoring tool that provides insights into your application's requests, database queries, cache operations, and more. This guide will show you how to set up and use Telescope effectively.

Prerequisites

  • Laravel 6.x or higher
  • Composer installed
  • Database configured
  • Basic understanding of Laravel

1 Install Laravel Telescope

Install Telescope via Composer:

composer require laravel/telescope --dev

Publish the configuration and migration files:

php artisan telescope:install
php artisan migrate

2 Configure Telescope

Update your `config/telescope.php` file:

return [
    'enabled' => env('TELESCOPE_ENABLED', true),
    'domain' => env('TELESCOPE_DOMAIN', null),
    'path' => env('TELESCOPE_PATH', 'telescope'),
    'storage' => [
        'database' => [
            'connection' => env('DB_CONNECTION', 'mysql'),
            'chunk' => 1000,
        ],
    ],
    'ignore_paths' => [
        //
    ],
    'ignore_commands' => [
        //
    ],
    'watchers' => [
        Watchers\CacheWatcher::class => [
            'enabled' => env('TELESCOPE_CACHE_WATCHER', true),
            'hidden' => [],
        ],
        Watchers\CommandWatcher::class => [
            'enabled' => env('TELESCOPE_COMMAND_WATCHER', true),
            'ignore' => [],
        ],
        Watchers\DumpWatcher::class => [
            'enabled' => env('TELESCOPE_DUMP_WATCHER', true),
        ],
        Watchers\EventWatcher::class => [
            'enabled' => env('TELESCOPE_EVENT_WATCHER', true),
            'ignore' => [],
        ],
        Watchers\ExceptionWatcher::class => [
            'enabled' => env('TELESCOPE_EXCEPTION_WATCHER', true),
        ],
        Watchers\JobWatcher::class => [
            'enabled' => env('TELESCOPE_JOB_WATCHER', true),
            'ignore' => [],
        ],
        Watchers\LogWatcher::class => [
            'enabled' => env('TELESCOPE_LOG_WATCHER', true),
        ],
        Watchers\MailWatcher::class => [
            'enabled' => env('TELESCOPE_MAIL_WATCHER', true),
            'outgoing' => true,
        ],
        Watchers\ModelWatcher::class => [
            'enabled' => env('TELESCOPE_MODEL_WATCHER', true),
            'events' => ['eloquent.*'],
            'hydrations' => true,
        ],
        Watchers\NotificationWatcher::class => [
            'enabled' => env('TELESCOPE_NOTIFICATION_WATCHER', true),
        ],
        Watchers\QueryWatcher::class => [
            'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
            'ignore_packages' => true,
            'slow' => 100,
        ],
        Watchers\RedisWatcher::class => [
            'enabled' => env('TELESCOPE_REDIS_WATCHER', true),
        ],
        Watchers\RequestWatcher::class => [
            'enabled' => env('TELESCOPE_REQUEST_WATCHER', true),
            'size_limit' => env('TELESCOPE_RESPONSE_SIZE_LIMIT', 64),
            'ignore_status_codes' => [],
        ],
        Watchers\ScheduleWatcher::class => [
            'enabled' => env('TELESCOPE_SCHEDULE_WATCHER', true),
        ],
        Watchers\ViewWatcher::class => [
            'enabled' => env('TELESCOPE_VIEW_WATCHER', true),
        ],
        Watchers\GateWatcher::class => [
            'enabled' => env('TELESCOPE_GATE_WATCHER', true),
            'ignore_abilities' => [],
            'ignore_packages' => true,
        ],
    ],
];

3 Configure Access

Update your `app/Providers/TelescopeServiceProvider.php`:

protected function gate()
{
    Gate::define('viewTelescope', function ($user) {
        return in_array($user->email, [
            'admin@example.com',
        ]);
    });
}

4 Using Telescope

Access Telescope at:

http://your-app.test/telescope

Monitor various aspects of your application:

  • Requests and responses
  • Database queries
  • Cache operations
  • Queue jobs
  • Mail notifications
  • Exceptions and errors

5 Pruning Data

Add the prune command to your scheduler in `app/Console/Kernel.php`:

protected function schedule(Schedule $schedule)
{
    $schedule->command('telescope:prune')->daily();
}

Key Features Covered

  • Installation and setup
  • Configuration options
  • Access control
  • Data monitoring
  • Data pruning
  • Performance tracking

Best Practices

  • Disable in production
  • Regular data pruning
  • Restrict access
  • Monitor performance impact
  • Use for debugging only

Common Issues

  • Performance overhead
  • Storage space issues
  • Access permission problems
  • Configuration conflicts
  • Database connection issues