Logging Context Enhancements in Laravel 8.49
Published on by Paul Redmond
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:
Today's Laravel 8.49.0 release includes a new "Log::withContext" method that adds contextual information to all subsequent log entries. Check it out! 📝https://t.co/IiC6asbBDi
— Taylor Otwell 🍜 (@taylorotwell) June 29, 2021
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, middlewarephp 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.