Middleware for adding a request ID to your Symfony requests.
First, add this project to your project's composer.json
$ composer require darsyn/stack-request-id ^1.0
Update your app.php
to include the middleware:
Before:
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel($env, $debug);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
After:
use Darsyn\Stack\RequestId\Injector;
use Darsyn\Stack\RequestId\UuidGenerator;
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel($env, $debug);
// Stack it! Node name is optional.
$generator = new UuidGenerator($nodeName);
$stack = new RequestId($kernel, $generator);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $stack->handle($request);
$response->send();
$kernel->terminate($request, $response);
If you use Symfony's MonologBundle you can add the request ID to your Monolog logs by adding the following service definition to your services.yml file:
services:
darsyn.stack.request_id.monolog_processor:
class: Darsyn\Stack\RequestId\Monolog\Processor
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 255 }
- { name: monolog.processor }
The default is X-Request-Id
.
$stack = new RequestId($kernel, $generator, 'Request-Id');
$stack = new RequestId($kernel, $generator, null, false);