Log and alert on failed Laravel queue jobs
Laravel puts a failed job in the failed_jobs table and tells nobody. Log every failure with its job class and exception, and get an alert when one fails.
Before you start
- A Laravel application with queue workers
- The tailcove/laravel package sending logs
When a queued job runs out of attempts, Laravel writes it to the failed_jobs table and moves on. Nobody is told. You find out when a customer asks where their invoice went, then SSH in and run php artisan queue:failed to see what broke. One setting in the Tailcove package turns every failure into an error line with the job's class and exception, which you can search, group and alert on.
On this page8 sections
What Laravel does with a failed job#
A job fails for good when it has used up its attempts (--tries on the worker, or $tries on the job), when it throws more exceptions than its $maxExceptions, or when it calls $this->fail(). Laravel then stores the job, its payload and the exception in failed_jobs, where php artisan queue:failed lists them and php artisan queue:retry puts them back on the queue. It also calls the job's failed() method if it has one, and fires the Illuminate\Queue\Events\JobFailed event.
The attempts before that aren't silent: each one that throws goes through your exception handler, so its exception is logged like any other. That's useful for debugging, but it's not the line to alert on, because a job that succeeds on its third try logged two exceptions and did its work.
The table is a good record. It just sits in the database until someone looks.
1. Turn on failed-job logging#
With the package installed and sending logs (see Laravel logging if it isn't yet), add one line to .env:
TAILCOVE_LOG_FAILED_JOBS=true
Queue workers read their config once, when they start. After changing .env, rebuild the config cache if you use one, and restart the workers:
php artisan config:cache
php artisan queue:restart
On Horizon, run php artisan horizon:terminate instead and let your process manager start it again.
The package listens for JobFailed and logs through the tailcove channel directly, so this works even if tailcove isn't in your LOG_STACK.
2. Find failures in the logs#
Each failure is one line at the error level with the message Queued job failed. Its context holds the queue connection, the job's class and the exception, plus the fields the package adds to every line:
{
"connection": "redis",
"job": "App\\Jobs\\SendInvoice",
"exception": {
"class": "App\\Exceptions\\InvoiceProviderUnavailable",
"message": "Invoice provider returned 503",
"location": "/var/www/app/Jobs/SendInvoice.php:38",
"trace": [
"#0 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\\Jobs\\SendInvoice->handle()"
]
},
"environment": "production",
"hostname": "worker-1",
"request_id": "9b3f1c2e-6a7d-4d1e-8f0a-2c5b7e4d1a93"
}
Search for them on the source's Logs tab:
"Queued job failed"
"Queued job failed" job:App\Jobs\SendInvoice
"Queued job failed" connection:redis host:worker-1
Because the line carries the exception, failures with the same exception thrown from the same place are grouped into one issue. A job that failed 200 times overnight is one row with a count of 200, and you can mark it resolved once it's fixed. If it comes back after that, the issue reopens by itself.
3. Get an alert when a job fails#
On the source's Alerts tab, create an alert:
| Field | Value |
|---|---|
| Query | "Queued job failed" |
| Threshold | 1 |
| Window | 5 minutes |
| Cooldown | 30 minutes |
With a threshold of 1, the first failure is enough. Some jobs are allowed to fail now and then, like a webhook to a customer's server that's down. Leave those out with "Queued job failed" -job:App\Jobs\DeliverWebhook and give them their own alert with a higher threshold.
Then choose who to tell on the Who to tell step. Slack, email, Discord and the rest are all there; the Slack guide walks through it.
4. Log what the job was working on#
The line names the job but not its arguments. To see which order or customer it was handling, log that yourself in the job's failed() method:
use Illuminate\Support\Facades\Log;
use Throwable;
public function failed(?Throwable $exception): void
{
Log::error('Invoice was not sent', [
'order_id' => $this->order->id,
'exception' => $exception,
]);
}
This one goes through your default stack, so tailcove needs to be in LOG_STACK. Open the line in Tailcove and the order id is right there next to the exception.
Scheduled tasks too#
A scheduled command that fails never reaches failed_jobs. Laravel fires ScheduledTaskFailed when a task throws, or when a command run in the foreground exits with a non-zero code. Log those as well:
TAILCOVE_LOG_SCHEDULE_FAILURES=true
Each one is an error line with the message Scheduled task failed, the command and the exception, so "Scheduled task failed" works as an alert query in the same way. A task started with runInBackground() doesn't report its exit code this way.
FAQ#
Where does Laravel log failed jobs?
In the failed_jobs database table, which php artisan queue:failed lists. The exception from each attempt also goes through your exception handler, so it lands in your normal log channel (storage/logs/laravel.log by default). Laravel doesn't notify anyone on its own.
How do I get notified when a Laravel job fails?
Set TAILCOVE_LOG_FAILED_JOBS=true so every failure becomes a Queued job failed line, then create a Tailcove alert on "Queued job failed" with a threshold of 1 and a Slack, email or other channel.
Does this work with Horizon?
Yes. Horizon runs ordinary queue workers, which fire the same JobFailed event. Restart it with php artisan horizon:terminate after changing .env.
Will I get an alert for every retry?
Not with the query above. Queued job failed is logged once, when Laravel gives up on the job. The exceptions from earlier attempts are logged too, but they don't match that query.
Can I retry a failed job from Tailcove?
No. Tailcove shows you what failed and why; retrying is still php artisan queue:retry on your side, or Horizon's dashboard.
Next steps#
- Read Laravel logs in production without SSH, for everything else your workers log.
- Every option in the Laravel package reference.
- How error tracking groups exceptions into issues.
Ready to try it on your own logs?
The Free plan includes a source, live tail, search and alerts. No credit card.