Laravel Telescope is a powerful debugging assistant for Laravel applications. Developed by the creators of Laravel, it provides insight into the internal workings of your application, making it easier to identify and fix issues. This article will cover what Laravel Telescope is, its features, and how to install, configure, and use it effectively.
What is Laravel Telescope?
Laravel Telescope is an elegant debugging assistant that offers real-time insights into your application’s activities. It provides a comprehensive overview of your application’s requests, exceptions, database queries, scheduled tasks, cache operations, and more. With its intuitive interface, Telescope makes it easier to monitor and debug your Laravel applications.
Key Features of Laravel Telescope
- Request Monitoring: Tracks all incoming HTTP requests to your application.
- Command Monitoring: Monitors Artisan commands executed within your application.
- Queue Monitoring: Tracks queued jobs and their execution details.
- Exception Monitoring: Captures and displays all exceptions thrown within your application.
- Database Query Monitoring: Logs all database queries executed by your application.
- Scheduled Task Monitoring: Tracks scheduled tasks defined in your
console/kernel.php
. - Cache Monitoring: Monitors cache operations like put, get, and forget.
- User Monitoring: Tracks user activities within your application.
- Model Monitoring: Monitors model events like created, updated, and deleted.
- Notification Monitoring: Tracks notifications sent within your application.
Installing Laravel Telescope
Step 1: Require Telescope via Composer
First, you need to require Laravel Telescope using Composer. Run the following command in your project directory:
composer require laravel/telescope
Step 2: Install Telescope
Next, install Telescope by running the installation command. This will publish Telescope’s assets and configuration file.
php artisan telescope:install
Step 3: Migrate the Database
Telescope requires a database table to store its data. Run the migrations to create the necessary tables:
php artisan migrate
Step 4: Configuring Telescope
Telescope’s configuration file is located at config/telescope.php
. You can customize various settings, such as storage options, filtering, and more. For example, you can set up Telescope to only run in specific environments:
'environments' => ['local', 'staging'],
Using Laravel Telescope
Once installed and configured, you can access Telescope via the /telescope
URL in your application. The Telescope dashboard provides a user-friendly interface to explore various aspects of your application.
Request Monitoring
The Requests tab displays all incoming HTTP requests, including details like request method, URL, status code, duration, and more. You can filter requests based on various criteria to find specific ones.
Command Monitoring
The Commands tab shows all Artisan commands executed within your application. This is useful for tracking scheduled tasks and manual command executions.
Queue Monitoring
The Jobs tab lists all queued jobs along with their statuses. You can see job details, execution time, and any associated exceptions.
Exception Monitoring
The Exceptions tab captures and displays all exceptions thrown in your application. Each exception entry includes the stack trace and contextual information to help you debug the issue.
Database Query Monitoring
The Queries tab logs all database queries executed by your application. It shows the query, bindings, duration, and the request that triggered the query. This helps in identifying slow queries and optimizing database performance.
Scheduled Task Monitoring
The Schedule tab tracks scheduled tasks defined in your console/kernel.php
file. You can see the task details, execution history, and status.
Cache Monitoring
The Cache tab monitors all cache operations, including put, get, forget, and flush. This is useful for ensuring your caching strategy is working as expected.
User Monitoring
The Users tab tracks user activities, such as login attempts, profile updates, and other significant actions. This helps in auditing user behavior and ensuring security.
Model Monitoring
The Models tab monitors model events like created, updated, and deleted. This is useful for tracking changes to your Eloquent models and ensuring data integrity.
Notification Monitoring
The Notifications tab logs notifications sent within your application. You can see the notification type, recipient, and content.
Advanced Configuration
Laravel Telescope offers several advanced configuration options to tailor its behavior to your needs.
Filter Telescope Entries
You can filter which entries Telescope records based on specific conditions. For example, you might only want to log requests from authenticated users:
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
Telescope::filter(function (IncomingEntry $entry) {
if ($entry->type === 'request') {
return auth()->check();
}
return true;
});
Batch Processing
For high-traffic applications, you might want to process Telescope entries in batches to improve performance. You can configure this in the config/telescope.php
file:
'entries' => [
'batch_size' => 100,
],
Pruning Old Entries
To prevent Telescope from using too much storage, you can configure it to prune old entries. This can be done in the config/telescope.php
file:
'prune' => [
'enabled' => true,
'age' => 24, // Prune entries older than 24 hours
],
Authorization
You can control access to the Telescope dashboard using the TelescopeServiceProvider
. By default, Telescope is restricted to local environments, but you can customize this:
use Laravel\Telescope\Telescope;
Telescope::auth(function ($request) {
return in_array($request->user()->email, [
'[email protected]',
]);
});
Integrating Telescope with Other Tools
Laravel Telescope can be integrated with other monitoring and alerting tools to provide a comprehensive overview of your application’s health.
Integrating with Slack
You can integrate Telescope with Slack to receive notifications about critical issues. For example, you can send notifications for exceptions:
use Illuminate\Support\Facades\Notification;
use App\Notifications\ExceptionOccurred;
Telescope::afterRecording(function (IncomingEntry $entry) {
if ($entry->type === 'exception') {
Notification::route('slack', 'https://hooks.slack.com/services/T000/B000/XXXX')
->notify(new ExceptionOccurred($entry));
}
});
Integrating with Sentry
Sentry is a popular error tracking tool that can be used alongside Telescope to provide detailed error reporting. To integrate Sentry with Laravel, follow these steps:
- Install the Sentry Laravel SDK:
composer require sentry/sentry-laravel
- Configure Sentry:
Add your Sentry DSN to the .env
file:
SENTRY_LARAVEL_DSN=https://[email protected]/0
- Initialize Sentry in the
AppServiceProvider
:
use Sentry\State\HubInterface;
use Sentry\Laravel\Integration;
public function register()
{
$this->app->singleton(HubInterface::class, function () {
return Sentry\init([
'dsn' => env('SENTRY_LARAVEL_DSN'),
'integrations' => [new Integration],
]);
});
}
Optimizing Performance
While Telescope is a powerful tool, it can introduce some overhead to your application. To minimize this impact, consider the following tips:
- Enable Batch Processing: As mentioned earlier, processing entries in batches can reduce overhead.
- Prune Old Entries: Regularly prune old entries to prevent database bloat.
- Optimize Database: Ensure your database is optimized and indexed appropriately to handle Telescope’s logging.
- Use Asynchronous Logging: Consider using a queue to log entries asynchronously, reducing the impact on request response times.
Conclusion
Laravel Telescope is an indispensable tool for developers who want to gain deeper insights into their Laravel applications. By providing detailed monitoring and logging of various application activities, Telescope makes debugging and performance optimization much easier. With its comprehensive feature set and intuitive interface, Laravel Telescope is a must-have for any serious Laravel developer.
By following this guide, you can install, configure, and effectively use Laravel Telescope to monitor and debug your applications. Remember to tailor Telescope’s settings to your specific needs and integrate it with other tools to create a robust monitoring and alerting system.