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;


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.


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();

            '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

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Любишь мемасики?

Подпишись на мой телеграм-канал!