如何在 Symfony 中使用 Monolog 断言记录了一行 [英] How to assert a line is logged using Monolog inside Symfony

查看:45
本文介绍了如何在 Symfony 中使用 Monolog 断言记录了一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Symfony2 中使用 Monolog,使用默认的 MonologBu​​ndle.我试图在我的测试中断言,记录了一行.我已经在我的 config_test.yml 中配置了这个:

I'm using Monolog inside Symfony2, using the default MonologBundle. I'm trying to assert inside my tests, that a line is logged. I've configured this in my config_test.yml:

monolog:
    handlers:
        main:
            type:   test
            level:  debug

如何在我的测试中获得 Monolog 的 TestHandler 的结果(继承自 Symfony2 的 WebTestCase)?

How do I get to the results of Monolog's TestHandler in my tests (that inherit from Symfony2's WebTestCase)?

推荐答案

作为解决方案:

monolog 服务中获取所有处理程序并搜索测试处理程序.

Get all handlers from monolog service and search test handler.

foreach ($this->container->get('monolog')->getHandlers() as $handler) {
  if ($handler instanceof TestHandler) {
    $testHandler = $handler;
    break;
  }
}

if (!$testHandler) {
  throw new \RuntimeException('Oops, not exist "test" handler in monolog.');
}

$this->assertFalse($testHandler->hasCritical()); // Or another assertions

这篇关于如何在 Symfony 中使用 Monolog 断言记录了一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆