【问题标题】:Symfony - monolog specific handlerSymfony - 独白特定的处理程序
【发布时间】:2019-03-19 23:55:12
【问题描述】:

我正在 symfony 4.0 下构建一个电子商务应用程序。 应用程序变得越来越复杂,我想记录该过程。 我想将它记录在特定的日志文件中。

为此,我创建了一个新的独白处理程序: 独白.yaml:

monolog:
    handlers:
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: ["!event"]
        youshoes:
            level: info
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%YOUSHOES.log"
            channels: [youshoes]
    channels: ["main", "youshoes"]

我现在如何使用此日志。例如:

 $logger->info('Client has validated is cart');
 $logger->info('Payment is successful');
 $logger->info('Client is requesting for deliveries');
etc ....

要在正确的日志文件中..

非常感谢您的帮助。

皮埃尔。

【问题讨论】:

    标签: symfony4 monolog


    【解决方案1】:

    我找到了解决办法……

    monolog.yaml

    monolog:
        handlers:
            main:
                type: stream
                path: "%kernel.logs_dir%/%kernel.environment%.log"
                level: debug
                channels: ["!event", "!youshoes"]
            youshoes:
                level: info
                type: stream
                path: "%kernel.logs_dir%/%kernel.environment%YOUSHOES.log"
                channels: ["youshoes"]
    
            # uncomment to get logging in your browser
            # you may have to allow bigger header sizes in your Web server configuration
            #firephp:
            #    type: firephp
            #    level: info
            #chromephp:
            #    type: chromephp
            #    level: info
            console:
                type:   console
                process_psr_3_messages: false
                channels: ["!event", "!doctrine", "!console"]
    
        channels: ["youshoes"]
    

    services.yaml

    ...
        # saw in https://symfony.com/doc/current/service_container.html#services-wire-specific-service
        App\Service\YoushoesLog:
            arguments:
                # the '@' symbol is important: that's what tells the container
                # you want to pass the *service* whose id is 'monolog.logger.request',
                # and not just the *string* 'monolog.logger.request'
                $logger: '@monolog.logger.youshoes'
    

    服务/YoushoesLog.php

    <?php
    
    namespace App\Service;
    use Psr\Log\LoggerInterface;
    
    class YoushoesLog
    
    {
    
        private $logger;
    
        public function __construct(LoggerInterface $logger)
        {
            $this->logger = $logger;
        }
    
        public function info($message)
        {
            if ($this->logger->info($message)) return true;
            return false;
        }
    }
    

    任何控制器:

    use App\Service\YoushoesLog;
    
    class TestController extends Controller{
    public function test(YoushoesLog $logger){
        $logger->info("Client has sent order");
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2016-07-15
      相关资源
      最近更新 更多