Git Product home page Git Product logo

requestidbundle's Introduction

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

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.

requestidbundle's People

Contributors

chrisguitarguy avatar goetas avatar rlanting avatar

Watchers

James Cloos avatar Laurent WILLEMS avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.