You are here

class LogRotator in File Log 8

Same name and namespace in other branches
  1. 2.0.x src/LogRotator.php \Drupal\filelog\LogRotator

Log rotation cron service.

Hierarchy

Expanded class hierarchy of LogRotator

1 file declares its use of LogRotator
FileLogRotationTest.php in tests/src/Unit/FileLogRotationTest.php
1 string reference to 'LogRotator'
filelog.services.yml in ./filelog.services.yml
filelog.services.yml
1 service uses LogRotator
filelog.rotator in ./filelog.services.yml
Drupal\filelog\LogRotator

File

src/LogRotator.php, line 23

Namespace

Drupal\filelog
View source
class LogRotator {

  /**
   * The filelog settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * The datetime.time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * The filelog.file_manager service.
   *
   * @var \Drupal\filelog\LogFileManagerInterface
   */
  protected $fileManager;

  /**
   * The file_system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * LogRotator constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config.factory service.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   * @param \Drupal\Core\Utility\Token $token
   *   The token service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The datetime.time service.
   * @param \Drupal\filelog\LogFileManagerInterface $fileManager
   *   The filelog service.
   * @param \Drupal\Core\File\FileSystemInterface $fileSystem
   *   The file_system service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, StateInterface $state, Token $token, TimeInterface $time, LogFileManagerInterface $fileManager, FileSystemInterface $fileSystem) {
    $this->config = $configFactory
      ->get('filelog.settings');
    $this->state = $state;
    $this->token = $token;
    $this->time = $time;
    $this->fileManager = $fileManager;
    $this->fileSystem = $fileSystem;
  }

  /**
   * Check and rotate if necessary.
   *
   * @param bool $force
   *   Bypass the scheduler and force rotation.
   *
   * @return bool
   *   Returns TRUE if the rotation was successful.
   *
   * @throws \Drupal\filelog\FileLogException
   */
  public function run($force = FALSE) : bool {
    if ($force || $this
      ->shouldRun($this->time
      ->getRequestTime())) {
      return $this
        ->rotateFile();
    }
    return FALSE;
  }

  /**
   * Check if sufficient time has passed since the last log rotation.
   *
   * @param int $now
   *   The current timestamp.
   *
   * @return bool
   *   TRUE if the log file should be rotated now.
   */
  public function shouldRun($now) : bool {
    $last = $this->state
      ->get('filelog.rotation');
    switch ($this->config
      ->get('rotation.schedule')) {
      case 'monthly':
        return date('m', $last) !== date('m', $now);
      case 'weekly':
        return date('W', $last) !== date('W', $now);
      case 'daily':
        return date('d', $last) !== date('d', $now);
    }
    return FALSE;
  }

  /**
   * Rotate the log file.
   *
   * @throws \Drupal\filelog\FileLogException
   */
  protected function rotateFile() : bool {
    $logFile = $this->fileManager
      ->getFileName();
    $truncate = $this->config
      ->get('rotation.delete');
    $timestamp = $this->state
      ->get('filelog.rotation');
    if (!$truncate) {
      $destination = $this->token
        ->replace($this->config
        ->get('rotation.destination'), [
        'date' => $timestamp,
      ]);
      $destination = PlainTextOutput::renderFromHtml($destination);
      $destination = $this->config
        ->get('location') . '/' . $destination;
      $directory = dirname($destination);
      $this->fileSystem
        ->prepareDirectory($directory, $this->fileSystem::CREATE_DIRECTORY);
      if ($this->config
        ->get('rotation.gzip')) {
        $truncate = TRUE;
        $data = file_get_contents($logFile);
        if (!file_put_contents($destination . '.gz', gzencode($data))) {
          throw new FileLogException("Log file could not be compressed from {$logFile} to {$destination}.gz.");
        }
      }
      elseif (!rename($logFile, $destination)) {
        throw new FileLogException("Log file could not be moved from {$logFile} to {$destination}.");
      }
    }
    if ($truncate) {

      // Simply truncate the log file, to save some file-system operations.
      $file = fopen($logFile, 'wb');
      if (!$file || !fclose($file)) {
        throw new FileLogException("Log file {$logFile} could not be truncated.");
      }
    }
    $this->state
      ->set('filelog.rotation', $this->time
      ->getRequestTime());
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LogRotator::$config protected property The filelog settings.
LogRotator::$fileManager protected property The filelog.file_manager service.
LogRotator::$fileSystem protected property The file_system service.
LogRotator::$state protected property The state service.
LogRotator::$time protected property The datetime.time service.
LogRotator::$token protected property The token service.
LogRotator::rotateFile protected function Rotate the log file.
LogRotator::run public function Check and rotate if necessary.
LogRotator::shouldRun public function Check if sufficient time has passed since the last log rotation.
LogRotator::__construct public function LogRotator constructor.