Symfony中如何在Monolog日志添加处理器名称并传递handler参数
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.mainwith the ID of whatever handler you want to inject (check yourmonolog.yamlconfig for handler names). - Restrict Processor to a Handler: The
handler: maintag attribute ensures your processor only runs when themainhandler 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

