Developed with love by KnpLabs Hire us for your project!
37

RequestIdBundle

by chrisguitarguy

Add request ID's to your Symfony application.

Request ID Bundle

This adds request ID's to your Symfony application. Why? It's a great way to add
some additional information to logs and to present to users. For example, if an
exception is thrown you'll be able to show the user the request ID which they
can pass on to you to locate their specific issue.

Installation

Use Composer.

composer require chrisguitarguy/request-id-bundle

Then enable the bundle in your AppKernel.

use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            // ...
            new Chrisguitarguy\RequestId\ChrisguitarguyRequestIdBundle(),
        ];

        // ...

        return $bundles;
    }

    // ...
}

Configuration

# in app/config/config.yml

chrisguitarguy_request_id:
    # The header which the bundle inspects for the incoming request ID
    # if this is not set an ID will be generated and set at this header
    request_header: Request-Id

    # Whether or not to trust the incoming request header. This is turned
    # on by default. If true a value in the `Request-Id` header in the request
    # will be used as the request ID for the rest of the request. If false
    # those values are ignored.
    trust_request_header: true

    # The header which the bundle will set the request ID to on
    # the response
    response_header: Request-Id

    # The service key of an object that implements
    # Chrisguitarguy\RequestId\RequestIdStorage
    # optional, defaults to `SimpleIdStorage`
    storage_service: ~

    # The service key of an object that implements
    # Chrisguitarguy\RequestId\RequestIdGenerator
    # optional, defaults to a UUID v4 based generator
    generator_service: ~

    # Whether or not to add the monolog process (see below), defaults to true
    enable_monolog: true

    # Whether or not to add the twig extension (see below), defaults to true
    enable_twig: true

How it Works

When a request comes in, it's inspected for the Request-Id header. If present,
the value in that header will be used throughout the rest of the bundle. This
lets you use request ID's from somewhere higher up in the stack (like in the web
server itself).

If no request ID is found, one is generated by the RequestIdGenerator. The
default generator creates version 4 UUIDs.

On the way out out, the Request-Id header is set on the response as well using
the value described above.

The headers are configurable. See the configuration above.

Monolog Integration

There's a monolog Processor that adds the request ID to extra array on the
record. This can be turned off by setting enable_monolog to false in the
configuration.

To use the request ID in your logs, include %extra.request_id% in your
formatter. Here's a configuration example from this bundle's tests.

# http://symfony.com/doc/current/cookbook/logging/monolog.html#changing-the-formatter

services:
    request_id_formatter:
        class: Monolog\Formatter\LineFormatter
        arguments:
            - "[%%level_name%% - %%extra.request_id%%] %%message%%"

monolog:
    handlers:
        file:
            type: stream
            level: debug
            formatter: request_id_formatter

Twig Integration

Important: Twig ^1.38 or ^2.7 is required for the twig integration to work.

By default this bundle will add a global request_id function to your twig
environment. To disable this set enable_twig to false in the bundle
configuration.

Here's an example of a template.

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, World</title>
    </head>
    <body>
        <h1>{{ request_id() }}</h1>
    </body>
</html>

License

MIT. See the LICENSE file.

Copyright (c) 2015 Christopher Davis <http://christopherdavis.me>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  • Add a Changelog Entry for 3.0.2
    By chrisguitarguy, 2 months ago
  • Merge pull request #9 from jamiebarbosa/update-twig
    By web-flow, 2 months ago
  • Fix Some Deprecations From the Symfony Core
    By chrisguitarguy, 2 months ago
  • Require Twig ^1.38|^2.7
    By chrisguitarguy, 2 months ago
  • update twig functions to use new format
    By , 3 months ago
  • Add a Changelog Entry for 3.0.1
    By chrisguitarguy, 7 months ago
  • Merge pull request #7 from goetas/patch-1
    By web-flow, 7 months ago
  • add composer package type
    By goetas, 7 months ago
  • test response header
    By goetas, 7 months ago
  • Fix typo
    By web-flow, 7 months ago
  • Merge pull request #6 from rlanting/fix/response-priority
    By web-flow, 7 months ago
  • Increase priority of response listener so it runs before profiler
    By , 7 months ago
  • Merge pull request #5 from chrisguitarguy/fix_deprecations
    By web-flow, 8 months ago
  • Don't Use Bundle:Directory:action For Controllers
    By chrisguitarguy, 8 months ago
  • Require Most Recent Version of PHPUnit 7.X
    By chrisguitarguy, 8 months ago
  • Run Travis on PHP 7.2 and 7.3
    By chrisguitarguy, 8 months ago
  • Use a Custom Twig Template Path in Tests
    By chrisguitarguy, 8 months ago
  • Note the PHP 7.2 Requirement in the Upgrade Guide
    By chrisguitarguy, 8 months ago
  • Correctly Create TreeBuilder for Symfony 4.2+
    By chrisguitarguy, 8 months ago
  • Fix a Twig Bundle Deprecation Warning
    By chrisguitarguy, 8 months ago
  • Upgrade to PHPUnit 7.X
    By chrisguitarguy, 8 months ago
  • Require PHP 7.2
    By chrisguitarguy, 8 months ago
  • Merge pull request #3 from chrisguitarguy/symfony4
    By web-flow, 1 year ago
  • Mention the Typing Changes in the Upgrade Guide & Changelog
    By chrisguitarguy, 1 year ago
  • Allow a Nullable String in `RequestIdStorage::setRequestId`
    By chrisguitarguy, 1 year ago
  • Add a (Very Negative) Upgrade Guide
    By chrisguitarguy, 1 year ago
  • Update the Changelog for Version 3.0
    By chrisguitarguy, 1 year ago
  • Add Tests to Verify the Public Services
    By chrisguitarguy, 1 year ago
  • Add Tests for the Twig Extension
    By chrisguitarguy, 1 year ago
  • Re-Work Dependency Injection Names & Services
    By chrisguitarguy, 1 year ago