dongshi1868 2018-04-17 17:12
浏览 62

在Symfony的Monolog日志中添加处理程序名称

I'm using a custom monolog processor as described here and I'd like to add the handler name into my logs. The code snippet on above links shows the argument being passed as session. How can I pass in the handler argument?

    app.logger.session_request_processor:
        class: AppBundle\Logger\SessionRequestProcessor
        arguments:  ['@session']
        tags:
            - { name: monolog.processor, method: processRecord }
  • 写回答

1条回答 默认 最新

  • doudu161481 2018-05-29 13:16
    关注

    This is not as easy as it seems. Processor are either registered per logger or per handler.

    If the processor is registered in the logger, there is no way of knowing which handler the processor went through, as all handlers are executed after all processors were executed. If you register the processor on a handler, you always know which handler is executed, but registering a processor for every handler manually is quite cumbersome.

    In order to solve this, you could add a CompilerPass which does the manual work for you.

    Lets assume we have this processor

    class HandlerProcessor
    {
        /**
         * @var string
         */
        private $handlerName;
    
        public function __construct($handlerName)
        {
            $this->handlerName = $handlerName;
        }
    
        public function processRecord(array $record)
        {
            $record['extra']['handler_name'] = $this->handlerName;
    
            return $record;
        }
    }
    

    With this processor you can then create a compiler pass, which creates all the definitions for the different handlers monolog created

    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\DependencyInjection\Definition;
    use Symfony\Component\DependencyInjection\Reference;
    
    class HandlerProcessorPass implements CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            if (!$container->hasParameter('monolog.handlers_to_channels')) {
                return;
            }
    
            foreach ($container->getParameter('monolog.handlers_to_channels') as $handlerId => $channels) {
                $definition = new Definition(HandlerProcessor::class);
                $handlerDefinition = $container->getDefinition($handlerId);
    
                $handlerName = $handlerDefinition->getClass();
                $definition->addArgument($handlerName);
    
                $handlerProcessorId = 'handler_processor.' . $handlerId;
                $container->setDefinition($handlerProcessorId, $definition);
    
                $handlerDefinition->addMethodCall('pushProcessor', array(new Reference($handlerProcessorId)));
            }
        }
    }
    

    As compiler passes can be quite confusing if you never used them before, lets go line by line:

    if (!$container->hasParameter('monolog.handlers_to_channels')) {
        return;
    }
    

    This checks if the monolog bundle is registered. The parameter monolog.handlers_to_channels is added to the container in the MonologExtension. The parameter itself contains an array where the key is the name of the handler id and the value is an array with all channels this handler is registered to (or null if this handler is valid for all channels)

    foreach ($container->getParameter('monolog.handlers_to_channels') as $handlerId => $channels) {
        $definition = new Definition(HandlerProcessor::class);
        $handlerDefinition = $container->getDefinition($handlerId);
    

    Here we create a new definition of our HandlerProcessor and get the definition of the current processed handler.

    $handlerName = $handlerDefinition->getClass();
    $definition->addArgument($handlerName);
    

    This will fetch the class name of the handler (e.g. Monolog\Handler\SlackHandler) and adds it as a constructor argument to the HandlerProcessor

    $handlerProcessorId = 'handler_processor.' . $handlerId;
    $container->setDefinition($handlerProcessorId, $definition);
    
    $handlerDefinition->addMethodCall('pushProcessor', array(new Reference($handlerProcessorId)));
    

    And last but not least we create a new ID for the processor definition we just created at the beginning of the foreach loop and add it with the method call pushProcessor to our newly created processor.

    Now that everything is set up, you have to register this compiler pass in your container. I assume you are using symfony 2.x so you have to do that in your AppBundle

    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    
    class AppBundle extends Bundle
    {
        public function build(ContainerBuilder $container)
        {
            $container->addCompilerPass(new HandlerProcessorPass());
        }
    }
    

    Here are some additional links which might be helpful:

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么