arnedesmedt / request-id-bundle
将请求ID添加到您的Symfony请求中。
dev-main
2023-12-11 19:10 UTC
Requires
- php: ^7.4 || ^8.0
- ramsey/uuid: ^3.9 || ^4.3
- symfony/framework-bundle: ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/browser-kit: ^5.4 || ^6.0 || ^7.0
- symfony/css-selector: ^5.4 || ^6.0 || ^7.0
- symfony/monolog-bridge: ^5.4 || ^6.0 || ^7.0
- symfony/monolog-bundle: ^3.7
- symfony/phpunit-bridge: ^5.4 || ^6.0 || ^7.0
- symfony/templating: ^5.4 || ^6.0 || ^7.0
- symfony/twig-bundle: ^5.4 || ^6.0 || ^7.0
- symfony/yaml: ^5.4 || ^6.0 || ^7.0
- twig/twig: ^2.7 || ^3.0
Conflicts
- twig/twig: <2.7
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文件。