m6web/log-bridge-bundle

该包的最新版本(v10.2.0)没有可用的许可证信息。

SF2代理管理器日志

v10.2.0 2022-09-06 13:20 UTC

README

使用Monolog记录请求/响应的Symfony Bundle。

注意:此Bundle的实际版本支持 Symfony >= 4.4。如果您需要支持旧版本,请使用 ~7.0 发布版。

特性

  • 语义配置
  • sf2事件调度器集成
  • 日志请求过滤器

安装

使用composer

    "require": {
        "m6web/log-bridge-bundle": "~3.0"
    }

添加到您的AppKernel

    $bundles = [
    // ...
        new M6Web\Bundle\LogBridgeBundle\M6WebLogBridgeBundle(),
    //...
    ];

使用方法

# app/config.yml

m6_web_log_bridge:
    active_filters:
        - get_article_error
        - post_article_all
        - all_error
    filters:
            get_article_error:
                route: get_article
                routes: ['get_article']
                method: ['GET']
                status: [422, 500]
                level: 'error'
                options:
                    response_body: true # from add Response body content (with DefaultFormatter)
            post_article_all:
                route: post_article
                routes: ['post_article']
                method: ~ # from all methods
                status: ~ # from all status
            get_article_not_found:
                route: get_article
                routes: ['get_article']
                method: ['GET']
                status: [404]
                level: 'warning'
            edit_category:
                route: get_category
                routes: ['get_category']
                method: ['POST', 'PUT']
                status: [400-422, ^510, !530-550]
                level: 'error'
                options:
                    post_parameters: true # From add post parameters in response content (with DefaultFormatter)
            all_error: # All route, all method in error
                route: ~
                routes: ~
                method: ~
                status: [31*, 4*, 5*]
                level: 'critical'
    content_formatter: m6web_log_bridge.log_content_formatter # Provider service name
    ignore_headers: # key list from mask/ignore header info
        - php-auth-pw
    prefix_key: ~ # define prefix key on log context
    logger: 
        channel: my_channel_to_log # monolog channel, optional, default 'log_bridge'

路由支持多种格式

routes: ['my_route'] # Add only this route
routes: ['my_route', 'another_route'] # Add multiples routes
routes: ['!excluded_one', '!excluded_two'] # Add all routes except the excluded

默认情况下,levelinfo

您可以声明所有需要的选项。默认情况下,只有 response_bodypost_parameters 由DefaultFormatter支持

状态支持多种格式

status: [401] # Add status 401
status: [^456] # Add status hundred greater than 450 (456, 457, 458, ..., 499)
status: [4*] # Add status hundred (200, 400, 401, 402, ..., 499)
status: [41*] # Add status decade (410, 411, 412, ..., 419)
status: [425-440] # Add range status (425, 426, 427, ..., 440)
status: [2*, 301, !203-210] # Add status (200, 201, 202, 211, ..., 299, 301)

可以使用 ! 来排除状态而不是添加

默认情况下,此Bundle使用内置的具有Monolog支持的logger m6web_log_bridge.logger。您可以编写自己的logger来覆盖此配置,该logger必须实现 Psr\Log\LoggerInterface

# app/config.yml

m6_web_log_bridge:
    logger: 
        service: acme.logger
services:
    acme.logger:
        class: Acme\DemoBundle\Logger\logger
        arguments: ["@logger"]
        tags:
            - { name: monolog.logger, channel: log_bridge }

Acme\DemoBundle\Logger\logger必须实现Psr\Log\LoggerInterface

从日志内容格式定义您的Provider

建议您扩展默认的Provider M6Web\Bundle\LogBridgeBundle\Formatter\DefaultFormatter

服务提供者默认定义

    services:
        m6web_log_bridge.log_content_provider:
            class: '%m6web_log_bridge.log_content_provider.class%'
            arguments:
                - '%kernel.environment%'
                - '%m6web_log_bridge.ignore_headers%'
                - '%m6web_log_bridge.prefix_key%'
            calls:
                - [ setTokenStorage, [ '@security.token_storage' ] ]

从覆盖

    services:
        acme.my_log_provider:
            class: Acme\Bundle\MyBundle\Provider\LogContentProvider
            parent: m6web_log_bridge.log_content_formatter

或者简单覆盖此参数: m6web_log_bridge.log_content_formatter.class

日志内容示例

Request
------------------------
content-type        : 
content-length      : 
host                : domain.tld
x-real-ip           : *********
x-forwarded-for     : *********
x-forwarded-proto   : http
x-forwarded-port    : 80
remote-user         : u_glinel
connection          : close
cache-control       : max-age=0
authorization       : Basic ************
accept              : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
user-agent          : Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/127.0.0.1 Chrome/127.0.0.1 Safari/537.36
accept-encoding     : gzip,deflate,sdch
accept-language     : fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
cookie              : PHPSESSID=************
php-auth-user       : u_glinel
x-php-ob-level      : 1
------------------------
Response
------------------------
HTTP 1.0 200
Age:           2
Etag:          
Vary:          
Cache-Control: no-cache
Content-Type:  application/json
Date:          dd mm yyyy hh:ii:ss GMT
------------------------
Response body
------------------------
Here response content

记录异常

该Bundle允许详细记录异常。这是通过在配置中使用 exception.log 来确保的。

# app/config.yml

m6_web_log_bridge:
    exception: 
        log: true
        request_attribute: LogBridgeException

此开关允许Bundle注册一个监听器,该监听器将保存任何接收到的异常,并将其传递给请求中定义的属性。

如果您使用默认的格式化程序,请使用配置进行更改。该Bundle提供了另一个能够记录异常的格式化程序实现。

# app/config.yml

m6_web_log_bridge:
    content_formatter: m6web_log_bridge.log_content_exception_formatter

如果您更喜欢使用自己的格式化程序,您将能够从请求中直接读取异常,属性在 m6_web_log_bridge.exception.request_attribute 中指定。

测试

您可以使用以下命令运行单元测试

    php bin/atoum -d src/M6Web/Bundle/LogBridgeBundle/Tests/Units