idct/symfony-async-monolog-handler

为 Symfony 6+ 框架和 Monolog 提供的一个简单工具链,允许异步发送日志。

0.1 2024-09-25 23:59 UTC

This package is auto-updated.

Last update: 2024-09-26 00:06:52 UTC


README

为 Symfony 6+ 框架和 Monolog 提供的一个简单工具链,允许异步发送日志。

安装

composer require idct/symfony-async-monolog-handler

使用

由于这不是一个包,而是一个作为工具链的库,您需要执行以下三个步骤才能使其真正工作

  1. 在您的 services.yaml 中注册服务
    monolog.handler.async-stream:
        class: IDCT\Logger\Handler\AsyncMessageHandler
        public: false
  1. 选择实际应该执行的处理程序。例如,如果您在 monolog.yaml 中有
    file_log:
        type: stream
        # log to var/log/(environment).log
        path: "%kernel.logs_dir%/async-test.log"
        # log *all* messages (debug is lowest level)
        level: debug

则将其添加到 services.yaml

    IDCT\Logger\Messenger\AsyncLogMessageHandler:
        arguments:
            - '@monolog.handler.file_log'
  1. 添加您的异步通道,例如命名为 async,然后将其添加到 monolog.yaml
monolog:
    channels:
        - async
  1. monolog.yaml 中注册您的异步代理记录器
    async-stream:
        type: 'service'
        id: 'monolog.handler.async-stream'
        channels: ['async']
        level: debug

其中 id 必须匹配步骤 1 中的服务标识符。

  1. 每次您想要使用您的异步记录器时,请使用标准的 symfony + monolog 命名约定注入它,包括通道的名称
    public function __construct(protected LoggerInterface $asyncLogger)
    {
        
    }
  1. messenger.yaml 中注册您的异步消息传输
framework:
    messenger:
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'

        routing:
            # Route your messages to the transports
            'IDCT\Logger\Model\AsyncLogMessage': async
  1. 确保设置 MESSENGER_TRANSPORT_DSN 环境变量。

  2. 激活 symfony/messenger。

  3. 注意:即使通道列表不匹配,目标处理程序也会处理消息。这可以用于过滤其他消息,例如,目标处理程序可能设置为忽略 messenger 日志

    file_log:
        type: stream
        # log to var/log/(environment).log
        path: "%kernel.logs_dir%/async-test.log"
        # log *all* messages (debug is lowest level)
        channels: ['!messenger']
        level: debug

贡献

任何有助于提高测试质量的贡献都受到欢迎。在 tests/func 中,您已经可以找到一个预配置的 symfony,当执行 app:test (TestCommand.php) 时,它将测试解决方案。