Getting started with Laravel Telescope — what can it do for you?
Tags: telescope, laravel, debugging
Published: 2019-07-08 17:41:24

---
title: Getting started with Laravel Telescope — what can it do for you?
published: true
tags: telescope,laravel,debugging
canonical_url: https://blog.logrocket.com/getting-started-with-laravel-telescope-what-can-it-do-for-you-719aaef07941
---

### Getting started with Laravel Telescope — what can it do for you?

![](https://cdn-images-1.medium.com/max/1024/1*ya66uWi8Ug21w5CLpIOvgA.png)

When building web applications, it is almost impossible to avoid encountering bugs. This could range from a typo to a forgotten semicolon or an undefined function. But most importantly, you want to have an idea of everything going on in your application, how and when they happen. Having access to this information gives you a lot more control in narrowing down bugs and identifying opportunities to improve your application.

[Laravel Telescope](https://laravel.com/docs/5.7/telescope) gives you exactly this power. It’s an amazing debugging assistant recently introduced in Laravel, that makes development a breeze with tools that allow you to not only monitor and debug various aspects of your application but also have access to a wide range of information that you’d normally not have direct access to.

Laravel Telescope provides insights into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache and a lot more. It also shows how different parts of your application work together. You can find out which pages are working right and which queries were run when a request was made to a certain page.

In this article, we will be covering the amazing features of Telescope, how to set it up and what it can do for you.

[![](https://cdn-images-1.medium.com/max/1024/1*GPjaPKNNUYHU8EsA3Z0JGA.png)](https://logrocket.com/signup/)

### Previous alternatives

If debugging in Laravel is not foreign to you, then chances are you may have used the following tools in the past to cater to your debugging needs.

#### Clockwork

[Clockwork](https://github.com/itsgoingd/clockwork) gives you an insight into your PHP application runtime, including request data, application log, database queries, cache usage, execution visualization and much more.

Clockwork provides a [Chrome](https://chrome.google.com/webstore/detail/clockwork/dmggabnehkmmfmdffgajcflpdjlnoemp) or [Firefox](https://addons.mozilla.org/en-US/firefox/addon/clockwork-dev-tools/) extension, or a web UI and a server-side component for gathering data that easily integrates with any PHP project, including out-of-the-box support for major frameworks.

#### Laravel Debugbar

[Laravel Debugbar](https://github.com/barryvdh/laravel-debugbar) adds a small bar at the bottom of the browser with debug information (e.g the number of queries). With Eloquent it can be easy to make unnecessary database requests so Debugbar helps to identify code that is not performant. It displays this right next to the developer console at the bottom of the browser.

#### Laravel log viewer

[Laravel log viewer](https://github.com/rap2hpoutre/laravel-log-viewer) allows you to see the logs from the storage/logs file in the browser as a pretty HTML table. The logs will be available at /logs.

### How does Laravel Telescope work?

Laravel Telescope provides a very powerful user interface for you to view, monitor and debug various aspects of your application. From requests to commands run in the terminal and even scheduled jobs. It does this through multiple watchers that have been configured to monitor these things.

These watchers have been designed to collect information about every request coming into your application, provide insight into database queries, how long they take to execute, exceptions encountered, events, queues, commands and more.

### Installation

Laravel Telescope was introduced in the latest Laravel version which is 5.7 and requires a minimum of Laravel 5.7.7 to work. At the time of this writing, Laravel is at version 5.7.14.

Once you have your Laravel application good to go, you can install Telescope through composer by running:

```
composer require laravel/telescope
```

You can also specify during installation if you want it to only work in your dev environment using the --dev flag. To do that, you’ll have to run:

```
composer require laravel/telescope --dev
```

Once done, you can publish the telescope assets and run migrations:

```
php artisan telescope:install

php artisan migrate
```

Laravel comes with two migrations already in place for you, the users table and the password\_resets table. When you run the migration, three tables are also created that handle telescope operations namely — telescope\_entries, telescope\_entries\_tags, and telescope\_monitoring.

### Features

Now, that we have installed and set up Telescope, visit /telescope on your project and you’ll arrive right in the telescope dashboard with tabs on the left listing pages that highlight the features.

![](https://cdn-images-1.medium.com/max/462/0*Zt4eRIz78JZgYTpf.png)

Each of the features listed above has its own page and watcher set up in Telescope. Now, we will go through what each of them lets us monitor and the details they provide.

### Requests

This feature page lists out all of the HTTP requests that come into your application and the time the request was made. It also provides additional information on each request by clicking on the eye icon to the right of each entry.

![](https://cdn-images-1.medium.com/max/480/0*NnXIwQWplUKjatQK.gif)

For each request made, you are also able to view related data from other watchers. For example, I ran a quick query to fetch all of the users on the homepage and it shows up in the requests along with how long that query took. This additional information is not limited to queries alone and can show information on any other watcher that was triggered by the request.

![](https://cdn-images-1.medium.com/max/1024/0*BpgLjVgKSBiylUfn.png)

### Commands

Telescope isn’t just limited to actions in the browser or requests but also covers Artisan commands run in the terminal as well. The commands page lists all of the commands that have been run in the terminal and shows if they were a success using their exit codes. Exit codes are usually 0 for when it’s a successful command and returns anything else ranging from 1 to 255 if another result was encountered. An example, as shown below, shows details of the php artisan migrate command ran in the terminal earlier while we were configuring telescope.

![](https://cdn-images-1.medium.com/max/1024/0*6GJlm_5R53MXUs3r.png)

### Schedule

Instead of having multiple cron entries for each task you need to schedule on your server, Laravel provides the scheduler that allows you to define these inside Laravel itself. The schedule page lists all of the commands for these scheduled tasks that have been run, providing information on when it happened and the cron schedule. If you have a command that provides an inspirational message at intervals using Laravel’s php artisan inspire, you can track the status each time this schedule is executed.

![](https://cdn-images-1.medium.com/max/1024/0*87gZ_X_-ibmg9Art.png)

### Jobs

The jobs page also lists out all of the jobs that have been run and their resulting status. Compared to Horizon which handles your Laravel powered Redis queues, Telescope works for all of the queue drivers included in Laravel. On the jobs details page, you can see the number of tries, timeouts, queue and the class name of the job ran etc. Here I have a SendWelcomeEmail job that is triggered anytime a new user signs up. I’m able to see that it failed and then find out why it did.

![](https://cdn-images-1.medium.com/max/1024/0*W7MXYTMe7Z_IoMmz.png)

### Exceptions

With Telescope, exceptions have become easy to look at. The exception page lists all of the exceptions and the number of times they occurred.

![](https://cdn-images-1.medium.com/max/1024/0*YT3ZAx0S50rM4eOq.png)

Moving on to the details page, it shows you the exact error message, what line in your code caused it and even the complete stack trace that is neatly formatted and fun to look at 😀.

![](https://cdn-images-1.medium.com/max/1024/0*3z4EtXcL-bxyxuDq.png)

### Logs

The logs page shows you a list of log messages, level and time each of them happened. Like all other pages, the individual details page shows more information including any context data you might have passed along. This totally beats having to go through the text files at storage/logs for log information.

![](https://cdn-images-1.medium.com/max/1024/0*TpYAPbfaaHnifAzE.png)

### Dumps

Most of the time, when faced with a bug, I just want to see the current state of variables or objects or any other information on the screen using the dump() method. Doing this normally results in the page formatting getting a bit distorted, but with Telescope, if you use the dump() method in your code, and you have the Dumps page open in Telescope, your page will appear normal and the output of the data will show up in the Dumps page instead.

![](https://cdn-images-1.medium.com/max/1024/0*icoWa6JtdmnQrALs.png)

One behavior I noticed with Dumps, however, is that if you try to dump a function that outputs a value directly, the dumps page will only show the returned value and the output will still show up on your page. For example:

```
function ace() {

    echo "Adewale";

    return false;

}

dump(ace());
```

Will show up on the dumps page as:

![](https://cdn-images-1.medium.com/max/1024/0*CioF7FZLR4TfiXba.png)

### Queries

The queries page works a lot like the Debugbar mentioned above. It lists all of your database queries, how long they took, on which requests etc. You can easily use this page to set a benchmark for which of your queries are considered slow once then take longer than a specific time. You can also set this in config/telescope.php. In the image attached below, I can see that select \* from ‘users’ query was run three times and had a different duration each time.

![](https://cdn-images-1.medium.com/max/1024/0*pSlfoKV9gbQZeNz_.png)

### Models

The Models page tracks all of the activities that go on with your models. The User model comes by default with Laravel. So as an example, every action taken on the User model will show up on this page. From when you create a new user or even update the user profile, it all shows up.

![](https://cdn-images-1.medium.com/max/1024/0*kdKhAb-1K3_qV0ZL.png)

![](https://cdn-images-1.medium.com/max/1024/0*l4xviQrWfFUhJt66.png)

### Events

The events page shows a list of all of your events with additional information to go with it. Including the time, listeners and which events were broadcasted with a tag.

![](https://cdn-images-1.medium.com/max/1024/0*5a2F7r0KQHd8DlAM.png)

### Mail, notifications, cache and redis

The mail page also shows a list of all the emails sent, who it was sent to, the time it was sent, the subject and even the preview of the email.

The notifications page shows a list of notifications, the channels they were created on, the time and who was the recipient of the notification.

The cache page shows a list of cache hits, misses, updates etc. You can see the request that triggered it and much more.

The redis page is also quite similar to the cache page. It shows you how long it took, when it happened and the request that triggered it.

### Additional configuration

Telescope publishes a config file located at config/telescope.php that you can use to configure the various watcher options and path. Each of these configurations come with a description of its purpose making it easy to understand what it’s meant to do.

As mentioned above, Telescope opens on /telescope by default in your application but you can change it to whatever suits you.

Telescope also allows you to specify who can access it in production, this is done by specifying a list of emails in the telescope service provider.

```
// app/Providers/TelescopeServiceProvider.php

...

/\*\*

_\* Register the Telescope gate._

_\*_

_\* This gate determines who can access Telescope in non-local environments._

_\*_

_\* @return void_

\*/

protected function gate() {

    Gate::define('viewTelescope', function ($user) {

        return in\_array($user->email, [

            //

        ]);

    });

}
```

### Should you use Telescope in production?

Yes, you can use Telescope in production. It has built-in authorization tools for protecting private data and you can even change the path as mentioned above to something else. For other protection techniques, you can filter out what data is monitored or stored, schedule jobs that delete old entries and also choose which of the watchers you want enabled or disabled.

### Conclusion

Telescope gives us great insight and overview of what goes on in Laravel applications and you should definitely start using it for all of your projects.

### Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps

[![](https://cdn-images-1.medium.com/max/1024/1*s_rMyo6NbrAsP-XtvBaXFg.png)](https://logrocket.com/signup/)<figcaption><a href="https://logrocket.com/signup/">https://logrocket.com/signup/</a></figcaption>

[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

[Try it for free.](https://logrocket.com/signup/)

* * *