Consent

This site uses third party services that need your consent.

Skip to content
Steven Roland

Supercharge Your Laravel Debugging with Telescope

Laravel Telescope is a powerful debugging and monitoring tool that provides deep insights into your Laravel application's behavior. It offers a beautiful dashboard that helps you inspect requests, exceptions, logs, database queries, and much more. In this post, we'll explore how to set up Telescope and leverage its features to enhance your development workflow.

Getting Started with Laravel Telescope

First, let's install Telescope via Composer:

composer require laravel/telescope --dev

After installation, publish Telescope's assets and run migrations:

php artisan telescope:install

php artisan migrate

Key Features and Examples

Request Monitoring

Telescope captures detailed information about incoming requests. You can view request parameters, headers, and response data.

Route::get('/api/users', function () {
    return User::all();
});

When you access this route, Telescope will log the request details, including the SQL query executed.

Exception Tracking

Telescope automatically captures exceptions, making it easier to debug issues.

Route::get('/error-demo', function () {
    throw new Exception('This is a demo exception');
});

Visit this route, and you'll see the exception details in Telescope's Exceptions tab.

Database Query Logging

Monitor your database queries, including execution time and bindings.

$users = DB::table('users')
    ->where('active', 1)
    ->get();

Telescope will log this query, showing you the exact SQL executed and its performance.

Job and Queue Monitoring

Track your queued jobs and their execution.

dispatch(new ProcessPodcast($podcast));

You can view the job's details, including its status and any exceptions thrown during execution.

Suggested Usages

  1. Performance Optimization: Use Telescope to identify slow database queries or inefficient API calls.

  2. Debugging Production Issues: Enable Telescope temporarily in production to diagnose hard-to-reproduce bugs.

  3. API Development: Monitor incoming API requests and responses to ensure correct data handling.

  4. Queue Management: Keep an eye on your job queues to ensure they're processing efficiently.

  5. Error Tracking: Use Telescope's exception logging to catch and fix errors quickly during development.

Best Practices

  • Secure Access: Always secure Telescope in production environments. You can use the gate method in the TelescopeServiceProvider:

    Telescope::gate(function ($request) {
        return in_array($request->user()->email, [
            '[email protected]',
        ]);
    });
  • Limit Data Collection: In busy applications, consider limiting the data Telescope collects:

    Telescope::filter(function (IncomingEntry $entry) {
        if ($entry->type === EntryType::REQUEST) {
            return $entry->content['method'] === 'POST';
        }
    
        return true;
    });
  • Use Tags: Tag entries to group related data:

    Telescope::tag(function (IncomingEntry $entry) {
        if ($entry->type === EntryType::REQUEST) {
            return ['status:' . $entry->content['response_status']];
        }
    
        return [];
    });
  • Regular Pruning: Set up automatic pruning to manage Telescope's database size:

    Telescope::prune(function (PrunableRepository $repository) {
        $repository->prune(now()->subMonth());
    });

Laravel Telescope is an invaluable tool for Laravel developers, offering deep insights into application behavior and performance. By integrating Telescope into your development workflow, you can catch issues early, optimize performance, and gain a better understanding of your application's inner workings.

Remember, while Telescope is incredibly useful during development, be cautious about using it in production environments due to its performance impact and the sensitive nature of the data it collects. Use it wisely, and happy debugging!

More posts

Simplifying Social Authentication with Laravel Socialite

Laravel Socialite simplifies OAuth authentication with social media platforms in Laravel apps. It offers easy setup and integration with providers like GitHub, Google, and Facebook. Key features include user detail retrieval and stateless authentication. Suggested uses include one-click registration, multi-provider auth, and social sharing. Best practices involve proper error handling, security measures, and token management.

Destined Connections: The Power of Shared Stories

Jandy Nelson's quote from "I'll Give You the Sun" explores the idea of destined connections in life's narrative. It encourages readers to consider how relationships and encounters might be part of a greater purpose or shared story.