chrisguitarguy/request-id-bundle

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

安装次数: 1,000,039

依赖项: 3

建议者: 0

安全: 0

星星: 38

观察者: 4

分支: 19

开放问题: 2

类型:symfony-bundle

v6.0.0 2024-01-19 19:51 UTC

README

这会将请求ID添加到您的Symfony应用程序中。为什么?这是一种向日志添加一些额外信息并向用户展示的绝佳方式。例如,如果抛出异常,您将能够向用户展示请求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 标头。如果存在,该标头中的值将用于整个捆绑包。这允许您使用来自堆栈更高处的请求ID(例如,在Web服务器本身中)。

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

在输出时,使用上述值将 Request-Id 标头设置在响应上。

这些标头是可以配置的。请参阅上面的配置

Monolog集成

有一个monolog Processor,它将请求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文件。