You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony中如何在Monolog日志添加处理器名称并传递handler参数

How to Pass a Handler Parameter to Your Custom Monolog Processor & Add Processor Name to Logs

Got it, let's walk through exactly how to adjust your setup to inject a handler into your custom processor and add the processor name to your log records. Here's what you need to do:

1. Update Your Custom Processor Class

First, modify your SessionRequestProcessor to accept a handler in its constructor, plus add a way to store and include the processor name in log records. Here's a revised version:

namespace AppBundle\Logger;

use Monolog\Handler\HandlerInterface;
use Monolog\Processor\ProcessorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionRequestProcessor implements ProcessorInterface
{
    private $session;
    private $handler;
    private $processorName;

    // Add HandlerInterface and optional processor name to constructor
    public function __construct(
        SessionInterface $session,
        HandlerInterface $handler,
        string $processorName = 'session_request_processor'
    ) {
        $this->session = $session;
        $this->handler = $handler;
        $this->processorName = $processorName;
    }

    public function processRecord(array $record): array
    {
        // Add your processor name to the log's extra fields
        $record['extra']['processor_name'] = $this->processorName;
        
        // Optional: Add handler details if needed (e.g., handler class name)
        // $record['extra']['handler_used'] = get_class($this->handler);

        // Keep your existing session logic
        if ($this->session->isStarted()) {
            $record['extra']['session_id'] = $this->session->getId();
        }

        return $record;
    }
}

2. Adjust Your Service Configuration to Inject the Handler

Update your Symfony service config to pass the desired Monolog handler into your processor. Monolog handlers are registered as services with the pattern monolog.handler.{handler_name} (e.g., the default main handler is @monolog.handler.main):

app.logger.session_request_processor:
    class: AppBundle\Logger\SessionRequestProcessor
    arguments:
        - '@session'
        - '@monolog.handler.main' # Inject your target handler service here
        - 'session_request_processor' # Optional: Override processor name if needed
    tags:
        # Optional: Add `handler: main` to restrict this processor to only the main handler
        - { name: monolog.processor, method: processRecord, handler: main }

Key Notes

  • Handler Service IDs: Replace @monolog.handler.main with the ID of whatever handler you want to inject (check your monolog.yaml config for handler names).
  • Restrict Processor to a Handler: The handler: main tag attribute ensures your processor only runs when the main handler processes logs. Omit this if you want it to apply to all handlers.
  • Processor Name Flexibility: You can hardcode the processor name in the class, pass it via config, or even derive it from the handler's properties (like its class name) if that fits your use case.

Verify the Result

After making these changes, your log records will include the processor name in the extra section. For example, a JSON log might look like this:

{
  "message": "User accessed dashboard",
  "level": 200,
  "level_name": "INFO",
  "extra": {
    "processor_name": "session_request_processor",
    "session_id": "xyz789abc"
  }
}

内容的提问来源于stack exchange,提问作者ghdna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:25:20