Laravel Nightwatch: Monitoring Your Application in Production
Explore how to use Laravel Nightwatch for production application monitoring
When a Laravel application moves from development to production, knowing that the application is running is not enough. Developers also need to know how it is performing, where errors are occurring, which database queries are slow, and whether background jobs are failing.
This is where Laravel Nightwatch comes in.
Nightwatch is Laravel's production monitoring and observability platform. It provides visibility into application activity such as HTTP requests, exceptions, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks and outgoing requests.
Getting Started
A Laravel application can be connected to Nightwatch with the Nightwatch package and agent.
First, install the package:
composer require laravel/nightwatch
Then start the Nightwatch agent:
php artisan nightwatch:agent
Your Nightwatch token is configured through the application's environment variables:
NIGHTWATCH_TOKEN=your-token-here
Once configured, application events can begin appearing in the Nightwatch dashboard. Laravel's original Nightwatch announcement describes this as a simple setup intended to get applications monitored quickly.
What Can You Monitor?
Nightwatch goes beyond simply displaying application logs.
For example, it can help developers investigate a slow request and see the database queries and other operations associated with that request.
A typical investigation might look like:
Request
└── Controller
├── Database Query
├── Database Query
└── External API Request
This makes it easier to determine whether a slow page is caused by application code, a database query, an external service or another part of the request lifecycle.
Nightwatch can also monitor queued jobs, exceptions, scheduled tasks, notifications, mail and cache activity.
Finding Performance Problems
Consider a Laravel route that retrieves a large number of orders:
public function orders()
{
return Order::with('customer')
->latest()
->get();
}
The code may work correctly, but production monitoring could reveal that the request is taking much longer than expected.
Nightwatch can help developers inspect the request timeline and identify expensive database operations or other bottlenecks.
This is particularly useful for finding problems such as slow queries and N+1 database queries.
Nightwatch vs Telescope
Laravel developers may already be familiar with Laravel Telescope.
The two tools serve different purposes.
Telescope is primarily useful for debugging and inspecting Laravel applications during development, while Nightwatch is designed for production observability, performance monitoring and error tracking.
In simple terms:
| Tool | Main Use |
|---|---|
| Telescope | Local development and debugging |
| Pulse | Application metrics and dashboards |
| Nightwatch | Production monitoring and observability |
Developers can therefore use Telescope during development and Nightwatch when they need deeper visibility into a live production application.
Deployment Tracking
Nightwatch can also associate application activity with deployments. This makes it easier to determine whether a newly deployed version introduced an error or performance problem.
For example:
php artisan nightwatch:deploy \
--ref=1a50baf \
--name="v1.2.3"
This allows a deployment to be connected with a particular release or commit.
Why It Matters
Production problems are often difficult to reproduce locally. A request may be slow only with real customer data, a queue job may fail only under heavy traffic, or a particular database query may become expensive as the application grows.
Nightwatch provides developers with the information needed to investigate these problems from the production environment rather than relying solely on user reports.
It is therefore particularly useful for Laravel applications that are growing beyond a small development environment.
Final Thoughts
Laravel Nightwatch adds another layer of visibility to the Laravel development workflow. Instead of simply knowing that an application has failed, developers can investigate what happened, where it happened and how the different parts of the application were connected.
For teams running Laravel applications in production, it can turn monitoring from an afterthought into part of the development and deployment process.
Nightwatch is available as a managed service, with a free tier for smaller applications and paid plans for applications requiring higher event volumes and longer data retention.