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
Performance Optimization: Use Telescope to identify slow database queries or inefficient API calls.
Debugging Production Issues: Enable Telescope temporarily in production to diagnose hard-to-reproduce bugs.
API Development: Monitor incoming API requests and responses to ensure correct data handling.
Queue Management: Keep an eye on your job queues to ensure they're processing efficiently.
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 theTelescopeServiceProvider
: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!