Log with context

New in Laravel 8.49: Log::withContext() will help you to differentiate the Log messages between different requests.

If you create a Middleware and set this context, all Log messages will contain that context, and you'll be able to search them easier.

1public function handle(Request $request, Closure $next)
2{
3 $requestId = (string) Str::uuid();
4 
5 Log::withContext(['request-id' => $requestId]);
6 
7 $response = $next($request);
8 
9 $response->header('request-id', $requestId);
10 
11 return $response;
12}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord