title: Rotate logs with standalone Monolog Library
body: <p>I work with a legacy project.
  I had added monolog logger as a standalone library (not with Symfony) and need to implement log rotation.</p>
  <p>Bellow is a Logger class and I am not sure how to implement log rotation functionality.</p>
  <pre><code>namespace Utils\Logger;
  
  use Exception;
  use Bootstrap;
  use Monolog\Handler\RotatingFileHandler;
  use Monolog\Logger as MonologLogger;
  use Monolog\Handler\StreamHandler;
  
  /**
   * Class Logger
   */
  class Logger
  {
      /** @var MonologLogger $logger */
      private $logger;
      const DEBUG    = MonologLogger::DEBUG;
      const NOTICE   = MonologLogger::NOTICE;
      const WARNING  = MonologLogger::WARNING;
      const ERROR    = MonologLogger::ERROR;
      const CRITICAL = MonologLogger::CRITICAL;
  
      public function __construct()
      {
          $bootstrap  = Bootstrap::getInstance();
          $projectDir = $bootstrap-&gt;getProjectDir();
          $host       = $bootstrap-&gt;initHost();
          $domain     = $host-&gt;getDomain();
  
          $this-&gt;logger = new MonologLogger($host-&gt;getDomain());
          $this-&gt;logger-&gt;
          $this-&gt;logger-&gt;pushHandler(
              new StreamHandler(
                  &quot;$projectDir/var/log/$domain/$domain.log&quot;,
                  MonologLogger::DEBUG)
          );
  
          $logRotate = new RotatingFileHandler(&quot;$projectDir/var/log/$domain/$domain.log&quot;);
      }
  
      /**
       * @param $level
       * @param $message
       * @param $context
       *
       * @return void
       */
      public function log($level, $message, array $context = [])
      {
          $message   = \is_string($message) ? $message : '';
          $exception = $this-&gt;getException($context);
  
          if ($exception) {
              $message = &quot;{$exception-&gt;getMessage()} at line {$exception-&gt;getLine()} in file {$exception-&gt;getFile()} Code: {$exception-&gt;getCode()}&quot;;
          }
  
          switch ($level) {
              case self::DEBUG:
                  $this-&gt;logger-&gt;debug($message);
                  break;
              case self::NOTICE:
                  $this-&gt;logger-&gt;notice($message);
                  break;
              case self::WARNING:
                  $this-&gt;logger-&gt;warning($message);
                  break;
              case self::ERROR:
                  $this-&gt;logger-&gt;error($message);
                  break;
              case self::CRITICAL:
                  $this-&gt;logger-&gt;critical($message);
                  break;
              default:
                  $this-&gt;logger-&gt;info($message);
                  break;
          }
      }
  
      /**
       * @param array $context
       *
       * @return Exception|null
       */
      private function getException($context)
      {
          return array_key_exists('exception', $context) &amp;&amp; $context['exception'] instanceof Exception
              ? $context['exception']
              : null;
      }
  }
  </code></pre>