Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Global contextual logging in Laravel 8.x

Logging is an integral part of developing applications. And if you’re working with Laravel, logging things is a breeze.

All you need is to use the Illuminate\Support\Facades\Log and utilize the following different logging levels like so.

use Illuminate\Support\Facades\Log;

Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);

As you can tell, the logging levels are self-explanatory. Apart from this, you can also pass an array of contextual data to the log methods This contextual data will be formatted and displayed with the log message like so.

use Illuminate\Support\Facades\Log;

Log::info('User failed to login.', ['id' => $user->id]);

Now, you can log things like this in an individual manner, sometimes you might want feasibility where you will always want certain information logged along with all your logs.

For instance, you will want to have user information every time you log something into your application. How would you do that?

Well, it turns out with the recent minor release of Laravel, you will be able to do just that.

Global contextual logging

Along with all the logging levels I mentioned previously, now in Laravel 8.x, you’ll also get a withContext method which you can use to include information that should be included with all subsequent log entries like so.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class AssignRequestId
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $requestId = (string) Str::uuid();

        Log::withContext([
            'request-id' => $requestId
        ]);

        return $next($request)->header('Request-Id', $requestId);
    }
}

As you can tell, you can use withContext in places from which are commonly accessible. For instance, in request middlewares.

Now, the next time you log anything, a request-id will also be logged along with the log message like so.

Laravel Global Contextual Logs

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?