0

I use Monolog\Logger in a service like this :

<service id="my_service" class="Acme\DemoBundle\Service\MyService">
    <tag name="monolog.logger" channel="mychannel" />
    <argument type="service" id="logger" />
    <argument type="service" ... />
</service>

In the service :

use Monolog\Logger;
class MyService{

private $logger;


public function __construct(Logger $logger, ...) {
    $this->logger = $logger;
    ...
}

}

I want to use the same channel that my service in my Controller so, in my controller, I have :

private function getLogger() {
    return $this->get('monolog.logger.mychannel');
}

I would like to retrieve records by level of my logs. How can I do this ?

Thanks you.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Gazelle
  • 11
  • 5

1 Answers1

0

I don't need to parse logs, I just want to know if there are errors or warnings.

I added this in my Controller :

private function hasLevelErrorHandler($type = "error") {
  $handlers = $this->getLoggger()->getHandlers();
  $records = $handlers[0]->getRecords();

  if( $type == "error" ) {
      $levels = array(Logger::ERROR, Logger::CRITICAL, Logger::ALERT,    Logger::EMERGENCY);
  } else {
      $levels = array(Logger::WARNING);
  }

  foreach( $records as $record ) {
      if( in_array($record['level'], $levels) ) {
          return true;
      }
  }
}

Is it a good way to do ? Thanks.

[EDIT] I don't have the same result with app.php and app_dev.php...

So, I don't know how can I parse my logs... If you have ideas...

Thanks you.

Gazelle
  • 11
  • 5