【问题标题】:How to write and use Monolog handlers and channels如何编写和使用 Monolog 处理程序和通道
【发布时间】:2015-08-31 13:14:57
【问题描述】:

我已经阅读了一些文档here,但我仍然不清楚如何编写和使用自定义 Monolog 处理程序和通道。让我解释一下我想要实现的目标。我有一个自定义函数,我希望将该日志记录到一个名为 custom.log 的文件中。我通过在config.yml 文件中设置它启用了 Doctrine 登录到另一个文件:

monolog:
    handlers:
        #Logs Doctrine to a different channel
        doctrine:
            level:    debug
            type:     stream
            path:     "%kernel.logs_dir%/doctrine.log"
            channels: [doctrine]

如何为custom.log 实现相同的目标?

【问题讨论】:

    标签: php symfony monolog symfony-2.6


    【解决方案1】:

    你可以试试这样,

    monolog:
        channels: ["testchannel"]
        handlers:
            test:
                # log all messages (since debug is the lowest level)
                level:    debug
                type:     stream
                path:     "%kernel.logs_dir%/testchannel.log"
                channels: ["testchannel"]
    

    在控制器中,您可以获取记录器并执行您的操作;

    class DefaultController extends Controller
    {
        public function indexAction()
        {
    
           $logger = $this->get('monolog.logger.testchannel');
           $logger->info("This one goes to test channel!!");
           return $this->render('AcmeBundle:Default:index.html.twig');
        }
    }
    

    您还可以通过运行命令php app/console container:debug monolog 来检查注册了哪些独白处理程序和记录器

    【讨论】:

      猜你喜欢
      • 2013-03-27
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 2013-02-10
      • 2014-02-02
      • 1970-01-01
      相关资源
      最近更新 更多