Logging Context Enhancements in Laravel 8.49


June 30th, 2021

Logging Context Enhancements in Laravel 8.49

The Laravel team released 8.49 with a logging context method that adds the same contextual information to subsequent logs during a request.

Logging With Context

Alex Harris contributed a new withContext() method to logging instances in order to add more detail to subsequent requests:

Let’s say that you want to correlate various logs together using a unique identifier. In the past, I’ve used the Laravel session ID to track the same user across logs. Here’s an example using the new Log::withContext() method:

1// Middleware example
4 * Handle an incoming request.
5 *
6 * @param \Illuminate\Http\Request $request
7 * @param \Closure $next
8 * @return mixed
9 */
10public function handle($request, Closure $next)
12 $requestId = (string) Str::uuid();
14 Log::withContext([
15 'request-id' => $requestId
16 ]);
18 return $next($request)->header('Request-Id', $requestId);

Check out the contexual information section of the logging documentation for details.

Get Status Text from an Illuminate Response

Taylor Maguire an accessor method statusText() to get the protected $statusText property from the Response instance:

1$response = new Response('foo');
4$response->statusText(); // i.e., Not Found

Sort By Route Resolution Order in route:list Command

Antonio Carlos Ribeiro contributed a new option to sort routes by resolution order (the default sort is by uri), which could be helpful to debug which route will resolve first:

1# Valid options are precedence, domain, method, uri, name, action, middleware
2php artisan route:list --sort precedence

Release Notes

You can see the complete list of new features and updates in the diff between 8.48.0 and 8.49.0 on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.