talentrydev/request-id-bundle

将请求ID添加到您的Symfony请求中。

安装: 1,813

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 19

类型:symfony-bundle

4.2.0 2021-03-18 13:59 UTC

This package is auto-updated.

Last update: 2024-09-18 21:55:40 UTC


README

此功能向您的Symfony应用程序添加请求ID。为什么?这是一个向日志和用户展示额外信息的好方法。例如,如果抛出异常,您将能够向用户展示请求ID,他们可以将此ID转交给您,以便定位他们的具体问题。

安装

使用Composer

composer require chrisguitarguy/request-id-bundle

然后在您的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;
    }

    // ...
}

配置

# 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

工作原理

当一个请求到来时,会检查其是否有Request-Id头。如果存在,则使用该头部的值在整个包中。这允许您从更高的层次(如Web服务器本身)使用请求ID。

如果没有找到请求ID,将通过RequestIdGenerator生成一个。默认生成器创建版本4的UUID。

在输出的过程中,将使用上述描述的值将Request-Id头设置在响应上。

头部是可以配置的。请参见上面的配置

Monolog集成

有一个monolog 处理器,它会将请求ID添加到记录的extra数组中。您可以通过在配置中将enable_monolog设置为false来关闭此功能。

要在日志中使用请求ID,请在格式化器中包含%extra.request_id%。以下是从此包的测试中获得的配置示例。

# https://symfony.com.cn/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集成

重要:要使twig集成工作,需要Twig ^2.7或^3.0。

默认情况下,此包将在您的twig环境中添加一个全局的request_id函数。要在包配置中禁用此功能,请将enable_twig设置为false

以下是一个模板示例。

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

许可证

MIT。请参阅LICENSE文件。