You are here

class UpdateLogger in Update helper 2.x

Same name and namespace in other branches
  1. 8 src/UpdateLogger.php \Drupal\update_helper\UpdateLogger

Helper service for logging in update hooks provided by update helper.

It provides output of logs to HTML, when update is executed over update.php. And it also provides output of logs for Drush command, when update is executed over drush command.

@package Drupal\update_helper

Hierarchy

  • class \Drupal\update_helper\UpdateLogger extends \Psr\Log\AbstractLogger

Expanded class hierarchy of UpdateLogger

1 string reference to 'UpdateLogger'
update_helper.services.yml in ./update_helper.services.yml
update_helper.services.yml
1 service uses UpdateLogger
update_helper.logger in ./update_helper.services.yml
Drupal\update_helper\UpdateLogger

File

src/UpdateLogger.php, line 19

Namespace

Drupal\update_helper
View source
class UpdateLogger extends AbstractLogger {

  /**
   * Container for logs.
   *
   * @var array
   */
  protected $logs = [];

  /**
   * The console logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $cliLogger;

  /**
   * The console output.
   *
   * @var string
   */
  protected $cliOutput = 'php://stderr';

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    $this->logs[] = [
      $level,
      $message,
      $context,
    ];
  }

  /**
   * Clear logs and returns currenlty collected logs.
   *
   * @return array
   *   Returns collected logs, since last clear.
   */
  protected function cleanLogs() {
    $logs = $this->logs;
    $this->logs = [];
    return $logs;
  }

  /**
   * Output logs in format suitable for HTML and clear logs too.
   *
   * @return string
   *   Returns HTML.
   */
  protected function outputHtml() {
    $full_log = '';
    $current_logs = $this
      ->cleanLogs();
    foreach ($current_logs as $log_entry) {
      $full_log .= $log_entry[1] . '<br /><br />';
    }
    return $full_log;
  }

  /**
   * Returns console logger.
   *
   * @return \Psr\Log\LoggerInterface
   *   Returns console logger.
   */
  protected function getCliLogger() {
    if (empty($this->cliLogger)) {
      $this->cliLogger = new ConsoleLogger(new StreamOutput(fopen($this->cliOutput, 'w'), OutputInterface::VERBOSITY_DEBUG));
    }
    return $this->cliLogger;
  }

  /**
   * Output logs in format suitable for console command and clear logs too.
   */
  protected function outputCli() {
    $console_logger = $this
      ->getCliLogger();
    foreach ($this
      ->cleanLogs() as $log_entry) {
      $console_logger
        ->log($log_entry[0], $log_entry[1]);
    }
  }

  /**
   * Output log result, depending on channel used and clean log.
   *
   * @return string
   *   Returns HTML string in case of non console execution.
   */
  public function output() {
    if (PHP_SAPI === 'cli') {
      $this
        ->outputCli();
      return '';
    }
    return $this
      ->outputHtml();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UpdateLogger::$cliLogger protected property The console logger.
UpdateLogger::$cliOutput protected property The console output.
UpdateLogger::$logs protected property Container for logs.
UpdateLogger::cleanLogs protected function Clear logs and returns currenlty collected logs.
UpdateLogger::getCliLogger protected function Returns console logger.
UpdateLogger::log public function
UpdateLogger::output public function Output log result, depending on channel used and clean log.
UpdateLogger::outputCli protected function Output logs in format suitable for console command and clear logs too.
UpdateLogger::outputHtml protected function Output logs in format suitable for HTML and clear logs too.