Unveiling Laravel Telescope: Boost Your Web Development Experience

Sauvik Kundu
5 min readJul 1, 2023

--

Unveiling Laravel Telescope: Boost Your Web Development Experience

Hello, fellow coders! Today we’re diving deep into Laravel Telescope, a powerful debugging assistant for Laravel, which simplifies the debugging and monitoring processes. We’ll walk through what it is, why it’s useful, how to use it, the best way to use it, practical examples, and managing it in a production environment.

What is Laravel Telescope?

Laravel Telescope is an elegant debug assistant for the Laravel framework. It provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps, and a lot more.

Why is Laravel Telescope Useful?

  1. Insightful Reporting: Laravel Telescope provides a comprehensive view of what’s happening within your application, whether it’s incoming requests, commands, queries, mails, notifications, cache, and more.
  2. Effective Debugging: By capturing detailed debug information, Telescope facilitates faster and more efficient debugging, thereby speeding up your development process.
  3. Environment Compatibility: It is flexible enough to be used in local development as well as in a production environment. The control you have over its functions provides safety and security in deploying Telescope in different environments.

How to Use Laravel Telescope

Installation

Start by installing Telescope through Composer:

composer require laravel/telescope

After the package is installed, you should publish the Telescope assets using the telescope:install Artisan command:

php artisan telescope:install

Then run the migration command:

php artisan migrate

And, voila! Laravel Telescope is ready to assist you!

Accessing Telescope

You can access Laravel Telescope by visiting the /telescope endpoint on your domain. This is where you'll find a wealth of information about your app's performance and functionality.

Best Ways to Use Laravel Telescope

Telescope offers a wide array of features. Here are some of the best ways to use it:

  1. Requests Monitoring: Use Telescope to keep an eye on incoming requests and their responses. This can be particularly helpful in identifying and resolving issues related to routes, middlewares, and responses.
  2. Queries Debugging: Telescope provides detailed reports about database queries, making it easier to optimize them and track down any potential issues.
  3. Jobs, Mails, and Events Monitoring: Use Telescope to monitor queued jobs, mails, and events within your application. It provides comprehensive details about these operations, including their input, output, status, and even failed jobs.
  4. Exception Handling: The Exception tab provides insights about all the exceptions occurring within your application. With stack trace information, you can debug the problem effectively.
  5. Performance Optimization: The Telescope profiler assists in identifying potential bottlenecks within your application, helping you to optimize its performance.

Examples

Here are some examples of how you can use Laravel Telescope in practice:

Debugging Database Queries

Telescope provides detailed insights about your app’s database queries. To see this, navigate to the “Queries” tab in the Telescope dashboard, where you can see the list of all executed queries, their location, and their execution time.

Monitoring Jobs

To monitor a job, navigate to the “Jobs” tab in Telescope. Here, you’ll see all the jobs dispatched by your application, along with details such as the connection, queue, payload, and status of each job.

Identifying Exceptions

Navigate to the “Exceptions” tab in Telescope to see all the exceptions that have occurred in your application. By clicking on each exception, you can see a detailed stack trace and the file where the exception occurred.

Using Telescope in a Production Environment

While Telescope is fantastic for local development, it can also be very effective in a production environment. However, given the sensitive nature of the data it exposes, it’s important to restrict access in a production setting.

To control access, you should define a gate before you register the TelescopeServiceProvider service provider. This gate determines who can access the Telescope in a production environment:

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

By doing this, only the specified users (in this case, ‘admin@yourdomain.com’) would be able to access Telescope’s data in a production environment.

Lastly, you might want to limit the entries stored by Telescope in a production environment. The telescope:prune Artisan command allows you to control this. The --hours option can be used to specify the number of hours of entries you wish to retain:

php artisan telescope:prune --hours=48

Wrapping Up: Production Environment and Laravel Telescope

As we conclude this deep dive into Laravel Telescope, it’s important to touch on some cautionary notes when it comes to using Telescope in a production environment.

While Laravel Telescope is an undeniably powerful tool for debugging and monitoring your Laravel application, it’s also important to be mindful of the implications it can have on your application’s performance and security when deployed in a production environment.

Firstly, Telescope can generate a substantial amount of data that could potentially impact the performance of your application, especially when it’s receiving a high volume of requests. You should be careful to balance the need for comprehensive debugging information against the potential impact on performance.

Secondly, Telescope provides very detailed information about the inner workings of your application, including database queries. This could potentially expose sensitive information if it falls into the wrong hands.

As a best practice, it’s often recommended to disable Telescope in a production environment, or at least limit its usage. If you still wish to use it, be sure to adequately protect access to it.

You can disable Telescope in production by setting the TELESCOPE_ENABLED environment variable in your .env file:

TELESCOPE_ENABLED=false

Or you can limit Telescope’s data collection in your TelescopeServiceProvider:

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

$this->app->configure('telescope');

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

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

In this example, Telescope will only record data for reportable exceptions, failed jobs, scheduled tasks, and entries with monitored tags in a production environment.

Remember, Laravel Telescope is a powerful tool, but with great power comes great responsibility. It’s essential to use it wisely and ensure that it enhances your application’s performance and security rather than hindering it. Happy coding!

Sauvik Kundu

--

--

Sauvik Kundu
Sauvik Kundu

Written by Sauvik Kundu

Experienced in developing scalable, reliable, and fast-performing web applications using PHP, with a focus on Laravel. https://www.linkedin.com/in/sauvik-kundu

No responses yet