arnedesmedt/request-id-bundle

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

安装: 0

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 19

类型:symfony-bundle

dev-main 2023-12-11 19:10 UTC

This package is auto-updated.

Last update: 2024-09-19 20:58:55 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 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文件。