title: How to Turn off Debugging Logs with Monolog
body: <p>I'm using Monolog in a project, it's not Symfony, just my own application that uses the stand-alone Monolog composer package.</p>
  
  <p>What I'd like to do is programmatically turn off debugging logs. I'm writing to a log file and I'm using the Monolog::StreamHandler. I'm controlling whether the application is in debug mode or not with a Configuration class that gets the debug value from a configuration file. So when someone changes that value to debugging is false, debug logging should turn off. </p>
  
  <p>I felt like the easiest way to do this would be to extend StreamHandler and override StreamHandler's write method like this.</p>
  
  <pre><code>class DurpLogger extends StreamHandler {
      protected function write(array $record) {
          if ($this-&gt;getLevel() == Durp::Debug &amp;&amp; !Configuration::debug()) {
              return;
          }
          parent::write($record);
      }
  }
  </code></pre>
  
  <p>So if a log request comes in and the log level for the handler is set to DEBUG and the application's Configuration::debug() is FALSE then just return without writing the log message. Otherwise, StreamHandler will do its thing.</p>
  
  <p>I'm wondering if this is the best way to use Monolog or if there's perhaps a cleaner way to do this.</p>
  
  <p>I envision there being a handler in my application for DEBUG, INFO, ERROR and whatever levels I might need for my application. Perhaps it makes sense to not rely on a Configuration::debug() that can only be TRUE or FALSE, but rather a Configuration::logLevel() that will allow me to more granularly control logging output. </p>
  
  <p>But even still, does extending StreamHandler make the most sense when controlling Monolog at the application level?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Now, I'm thinking something like this, that uses level rather than just boolean debug.</p>
  
  <pre><code>class DurpLogger extends StreamHandler {
      public function __construct() {
          parent::__construct(Configuration::logFile(), Configuration::logLevel());
      }
  
      protected function write(array $record) {
          if (!($this-&gt;getLevel() &gt;= Configuration::logLevel())) {
              return;
          }
          parent::write($record);
      }
  }
  </code></pre>
  
  <p>Then I'd use it in the application like this.</p>
  
  <pre><code>class Durp {
    private $logger;
    public function __construct() {
          $this-&gt;logger = new Logger('durp-service');
          $this-&gt;logger-&gt;pushHandler(new DurpLogger());
          $this-&gt;logger-&gt;addDebug('Debugging enabled');
          $this-&gt;logger-&gt;addInfo('Starting Durp');
      }
  }
  </code></pre>
  
  <p>I figured the StreamHandler handles the file writing stuff, so that's why I'm extending it. And if I turn up the log level in Configuration to Logger::INFO, the "Debugging enabled" message doesn't get logged.</p>
  
  <p>Open to suggestions to make this better.</p>