You are here

public function UltimateCronCommands::logs in Ultimate Cron 8.2

Show a cron jobs logs.

@command cron:logs

@option limit Number of log entries to show @option compact Only show the first line of each log entry @usage drush cron-logs node_cron --limit=20 Show 20 last logs for the node_cron job @aliases cron-logs @format table

Parameters

string $name: Job to show logs for.

array $options: Options array.

File

src/Commands/UltimateCronCommands.php, line 52

Class

UltimateCronCommands
Class UltimateCronCommands.

Namespace

Drupal\ultimate_cron\Commands

Code

public function logs($name, array $options = [
  'limit' => NULL,
  'compact' => NULL,
]) {
  if (!$name) {
    throw new \Exception(dt('No job specified?'));
  }

  /** @var \Drupal\ultimate_cron\Entity\CronJob $job */
  $job = CronJob::load($name);
  if (!$job) {
    throw new \Exception(dt('@name not found', [
      '@name' => $name,
    ]));
  }
  $compact = $options['compact'];
  $limit = $options['limit'];
  $limit = $limit ? $limit : 10;
  $table = [];
  $table[] = [
    '',
    dt('Started'),
    dt('Duration'),
    dt('User'),
    dt('Initial message'),
    dt('Message'),
    dt('Status'),
  ];
  $lock_id = $job
    ->isLocked();
  $log_entries = $job
    ->getLogEntries(ULTIMATE_CRON_LOG_TYPE_ALL, $limit);

  /** @var \Drupal\ultimate_cron\Logger\LogEntry $log_entry */
  foreach ($log_entries as $log_entry) {
    $progress = '';
    if ($log_entry->lid && $lock_id && $log_entry->lid === $lock_id) {
      $progress = $job
        ->getProgress();
      $progress = is_numeric($progress) ? sprintf(' (%d%%)', round($progress * 100)) : '';
    }
    $legend = '';
    if ($lock_id && $log_entry->lid == $lock_id) {
      $legend .= 'R';
      list(, $status) = $job
        ->getPlugin('launcher')
        ->formatRunning($job);
    }
    elseif ($log_entry->start_time && !$log_entry->end_time) {
      list(, $status) = $job
        ->getPlugin('launcher')
        ->formatUnfinished($job);
    }
    else {
      list(, $status) = $log_entry
        ->formatSeverity();
    }
    $table[$log_entry->lid][] = $legend;
    $table[$log_entry->lid][] = $log_entry
      ->formatStartTime();
    $table[$log_entry->lid][] = $log_entry
      ->formatDuration() . $progress;
    $table[$log_entry->lid][] = $log_entry
      ->formatUser();
    if ($compact) {
      $table[$log_entry->lid][] = trim(reset(explode("\n", $log_entry->init_message)), "\n");
      $table[$log_entry->lid][] = trim(reset(explode("\n", $log_entry->message)), "\n");
    }
    else {
      $table[$log_entry->lid][] = trim($log_entry->init_message, "\n");
      $table[$log_entry->lid][] = trim($log_entry->message, "\n");
    }
    $table[$log_entry->lid][] = $status;
  }
  return new RowsOfFields($table);
}