How to customize the log output for a job in Laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Customize Log Output for Jobs in Laravel: Mastering Context with Monolog

As a senior developer working with Laravel, we frequently rely on queues and scheduled jobs to handle asynchronous processing. When these jobs fail or produce unexpected results, debugging can become a nightmare if the logs lack sufficient context. Laravel leverages the powerful Monolog library under the hood for all its logging operations, which gives us immense flexibility in how we record events.

This post will walk you through exactly how to enrich your job logs, moving beyond generic processing messages to capture the specific data relevant to each job execution, allowing for much smarter debugging.

Understanding Laravel Job Logging

When a Laravel job is processed by a queue worker (like Supervisor), the output you see—such as Processing: App\Jobs\UpdateItemStatus—is typically generated by the queue system itself or basic framework logging. To add meaningful information, we need to explicitly inject our custom data into the Monolog records that the job generates.

The core idea is to use the logger instance available within your job to record specific details at the point of execution.

The Solution: Injecting Context via Monolog

To achieve the desired output—including item ID and status directly in the log stream—you need to access the logging mechanism inside your handle() method and utilize contextual data when calling the logging methods.

Let's look at how we can modify your ApiUpdateItemJob to include the necessary details.

Original Job Structure (Example)

Your current job handler focuses only on the business logic:

// ApiUpdateItemJob.php (Original)
public function handle()
{
    $data = [
        'id' => $this->item_id,
        'data[status]' => $this->status,
    ];

    $response = ApiFacedeClient::exec('Item', 'update', $data);

    $result = json_decode($response->getBody());
}

Implementing Contextual Logging

To see the detailed information you desire (Processing item id #333333 (Status [200 OK]):), you must use the injected Log facade or a specific logger instance to record this information. When logging, we pass an array of context data. This allows Monolog to structure the log entry with key-value pairs, which is far superior for searching and analysis than simple string concatenation.

Here is how you would modify your job handler:

use Illuminate\Support\Facades\Log;
use App\Jobs\UpdateItemStatus; // Assuming this is the class name

class ApiUpdateItemJob extends Job
{
    // ... properties and constructor ...

    public function handle()
    {
        $itemId = $this->item_id;
        $status = $this->status;

        // 1. Log the start of the process with context
        Log::info('Processing item', [
            'item_id' => $itemId,
            'status' => $status,
            'job_class' => get_class($this), // Good practice to log which job ran
        ]);

        $data = [
            'id' => $itemId,
            'data[status]' => $status,
        ];

        // Simulate the API call
        $response = ApiFacedeClient::exec('Item', 'update', $data);

        $result = json_decode($response->getBody());

        // 2. Log the result with context
        Log::info('Item update successful', [
            'item_id' => $itemId,
            'http_status' => $response->getStatusCode(),
            'result' => $result,
        ]);
    }
}

Deeper Dive into Monolog and Queue Logs

By implementing the above changes, when your queue worker processes this job, the logs will now contain structured data. Instead of just seeing a generic "Processing" message, you will see detailed entries like:

[YYYY-MM-DD HH:MM:SS] local.info: Processing item {"item_id": 333333, "status": "200 OK", "job_class": "App\Jobs\UpdateItemStatus"}

This structured format is invaluable for debugging. Tools that parse these logs (like Kibana or dedicated log analysis platforms) can easily query for all jobs related to item_id: 333333, instantly pinpointing the exact execution details without manual text parsing. This level of detail aligns perfectly with the principles of robust application design promoted by frameworks like Laravel, which encourages clean separation of concerns and reliable data handling (as seen in how Laravel structures its service layers).

Conclusion

Customizing log output for Laravel Jobs is not just about adding extra strings; it’s about adopting structured logging practices. By leveraging Monolog's capability to handle contextual data, you transform your logs from simple historical records into powerful diagnostic tools. Always remember that making your application debuggable is a core tenet of building scalable systems, and staying true to the principles of clean code, as advocated by the Laravel team, ensures long-term maintainability.