darsyn/stack-request-id

Middleware for adding a request ID to Symfony Request.

1.0.0 2016-02-12 13:33 UTC

This package is auto-updated.

Last update: 2020-09-15 18:18:21 UTC


README

Middleware for adding a request ID to your Symfony requests.

Build Status

Installation

First, add this project to your project's composer.json

$ composer require darsyn/stack-request-id ^1.0

Setting up

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 Injector($kernel, $generator);

$kernel->loadClassCache();

$request = Request::createFromGlobals();
$response = $stack->handle($request);
$response->send();
$kernel->terminate($request, $response);

Adding the RequestId to your Monolog logs

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 }

Changing the Response Header

The default is X-Request-Id.

$stack = new Injector($kernel, $generator, 'Request-Id');

Disabling the Response Header

$stack = new Injector($kernel, $generator, null, false);