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

How to write and use Monolog handlers and channels

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

中设置启用了 Doctrine 登录到另一个文件
monolog:
    handlers:
        #Logs Doctrine to a different channel
        doctrine:
            level:    debug
            type:     stream
            path:     "%kernel.logs_dir%/doctrine.log"
            channels: [doctrine]

如何为 custom.log 实现同样的效果?

你可以这样试试,

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

检查注册了哪些 monolog 处理程序和记录器