[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/daemon/management/ -> PhabricatorDaemonManagementLogWorkflow.php (source)

   1  <?php
   2  
   3  final class PhabricatorDaemonManagementLogWorkflow
   4    extends PhabricatorDaemonManagementWorkflow {
   5  
   6    public function didConstruct() {
   7      $this
   8        ->setName('log')
   9        ->setExamples('**log** [__options__]')
  10        ->setSynopsis(
  11          pht(
  12            'Print the logs for all daemons, or some daemon(s) identified by '.
  13            'ID. You can get the ID for a daemon from the Daemon Console in '.
  14            'the web interface.'))
  15        ->setArguments(
  16          array(
  17            array(
  18              'name'    => 'id',
  19              'param'   => 'id',
  20              'help'    => 'Show logs for daemon(s) with given ID(s).',
  21              'repeat'  => true,
  22            ),
  23            array(
  24              'name'    => 'limit',
  25              'param'   => 'N',
  26              'default' => 100,
  27              'help'    => 'Show a specific number of log messages '.
  28                           '(default 100).',
  29            ),
  30          ));
  31    }
  32  
  33    public function execute(PhutilArgumentParser $args) {
  34  
  35      $query = id(new PhabricatorDaemonLogQuery())
  36        ->setViewer($this->getViewer())
  37        ->setAllowStatusWrites(true);
  38      $ids = $args->getArg('id');
  39      if ($ids) {
  40        $query->withIDs($ids);
  41      }
  42      $daemons = $query->execute();
  43  
  44      if (!$daemons) {
  45        if ($ids) {
  46          throw new PhutilArgumentUsageException(
  47            pht('No daemon(s) with id(s) "%s" exist!', implode(', ', $ids)));
  48        } else {
  49          throw new PhutilArgumentUsageException(
  50            pht('No daemons are running.'));
  51        }
  52      }
  53  
  54      $console = PhutilConsole::getConsole();
  55  
  56      $limit = $args->getArg('limit');
  57  
  58      $logs = id(new PhabricatorDaemonLogEvent())->loadAllWhere(
  59        'logID IN (%Ld) ORDER BY id DESC LIMIT %d',
  60        mpull($daemons, 'getID'),
  61        $limit);
  62      $logs = array_reverse($logs);
  63  
  64      $lines = array();
  65      foreach ($logs as $log) {
  66        $text_lines = phutil_split_lines($log->getMessage(), $retain = false);
  67        foreach ($text_lines as $line) {
  68          $lines[] = array(
  69            'id' => $log->getLogID(),
  70            'type' => $log->getLogType(),
  71            'date' => $log->getEpoch(),
  72            'data' => $line,
  73          );
  74        }
  75      }
  76  
  77      // Each log message may be several lines. Limit the number of lines we
  78      // output so that `--limit 123` means "show 123 lines", which is the most
  79      // easily understandable behavior.
  80      $lines = array_slice($lines, -$limit);
  81  
  82      foreach ($lines as $line) {
  83        $id = $line['id'];
  84        $type = $line['type'];
  85        $data = $line['data'];
  86        $date = date('r', $line['date']);
  87  
  88        $console->writeOut(
  89          "%s\n",
  90          sprintf(
  91            'Daemon %d %s [%s] %s',
  92            $id,
  93            $type,
  94            $date,
  95            $data));
  96      }
  97  
  98      return 0;
  99    }
 100  
 101  
 102  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1