symfonyphpunitmonolog

How to assert a line is logged using Monolog inside Symfony


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

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


Solution

  • As solution:

    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