Logging Context Enhancements in Laravel 8.49

News

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:

// Middleware example
 
/**
* 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);
}

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:

$response = new Response('foo');
$response->setStatusCode(404);
 
$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:

# Valid options are precedence, domain, method, uri, name, action, middleware
php 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.