sbsedv / request-id-bundle
一个添加唯一请求ID的Symfony扩展包。
v1.7.0
2023-12-01 15:46 UTC
Requires
- php: ^8.2
- symfony/framework-bundle: ^6.3 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.4
- phpstan/phpstan-symfony: ^1.1
- symfony/http-client: ^6.3 || ^7.0
- symfony/monolog-bundle: ^3.6
- symfony/twig-bundle: ^6.3 || ^7.0
- symfony/uid: ^6.3 || ^7.0
- twig/twig: ^3.3
README
sbsedv/request-id-bundle
一个添加唯一请求ID的Symfony扩展包。
以下配置示例中,使用的是默认值。
通用配置如下
# config/packages/sbsedv_request_id.yaml sbsedv_request_id: generator: "SBSEDV\Bundle\RequestIdBundle\Generator\UuidRequestIdGenerator" # generator: "SBSEDV\Bundle\RequestIdBundle\Generator\RequestIdGenerator" # generator: 'your_custom_service_id' (must implement RequestIdGeneratorInterface) outgoing_http_header: "x-request-id" # http header that will be added # outgoing_http_header: false # disables the header creation incoming_http_header: false # disabled # incoming_http_header: "x-request-id" # request header that contains the Request-ID to use
当使用 SBSEDV\Bundle\RequestIdBundle\TrustStrategy\TrustStrategyInterface
服务信任传入请求时,才会使用传入的http-header。
默认注册了 PrivateIpTrustStrategy。
Twig集成
如果你的应用程序已安装symfony/twig-bundle,则注册了request_id
twig函数。
{# templates/example.html.twig #} <p>Request-ID: {{ request_id() }}</p>
你可以通过以下方式自定义注册的twig函数名称:
# config/packages/sbsedv_request_id.yaml sbsedv_request_id: twig_function_name: "request_id"
错误渲染器
默认情况下,此扩展包装饰了error_renderer
服务,并在</body>
标签前插入当前请求ID。
# config/packages/sbsedv_request_id.yaml sbsedv_request_id: error_renderer_decorator: false # enabled by default
Monolog集成
如果你的应用程序已安装symfony/monolog-bundle,则注册了一个日志处理器,将请求ID添加到每个记录中。
# config/packages/sbsedv_request_id.yaml sbsedv_request_id: monolog_processor: key: "request_id" # Key to which the request id will be set # monolog_processor: false # disables the processor