dongshan1811 2015-06-15 21:37
浏览 71
已采纳

如何编写和使用Monolog处理程序和通道

I have read some docs here but still not clear to me how to write and use a custom Monolog handler and channel. Let me explain a bit what I want to achieve. I have a custom function and I want that log to be logged into a file called custom.log. I have enabled Doctrine logging into another file by setting this in config.yml file:

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

How do I achieve the same for a custom.log?

  • 写回答

1条回答 默认 最新

  • duanjue7745 2015-06-16 00:16
    关注

    You can try that way,

    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"]
    

    And in the controller you can get the logger and do your thing;

    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');
        }
    }
    

    Also you can check which monolog handlers and loggers are registered by running the command php app/console container:debug monolog

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器