Path: blob/master/src/applications/daemon/management/PhabricatorDaemonManagementLogWorkflow.php
12256 views
<?php12final class PhabricatorDaemonManagementLogWorkflow3extends PhabricatorDaemonManagementWorkflow {45protected function didConstruct() {6$this7->setName('log')8->setExamples('**log** [__options__]')9->setSynopsis(10pht(11'Print the logs for all daemons, or some daemon(s) identified by '.12'ID. You can get the ID for a daemon from the Daemon Console in '.13'the web interface.'))14->setArguments(15array(16array(17'name' => 'id',18'param' => 'id',19'help' => pht('Show logs for daemon(s) with given ID(s).'),20'repeat' => true,21),22array(23'name' => 'limit',24'param' => 'N',25'default' => 100,26'help' => pht(27'Show a specific number of log messages (default 100).'),28),29));30}3132public function execute(PhutilArgumentParser $args) {3334$query = id(new PhabricatorDaemonLogQuery())35->setViewer($this->getViewer())36->setAllowStatusWrites(true);37$ids = $args->getArg('id');38if ($ids) {39$query->withIDs($ids);40}41$daemons = $query->execute();42$daemons = mpull($daemons, null, 'getID');4344if ($ids) {45foreach ($ids as $id) {46if (!isset($daemons[$id])) {47throw new PhutilArgumentUsageException(48pht(49'No log record exists for a daemon with ID "%s".',50$id));51}52}53} else if (!$daemons) {54throw new PhutilArgumentUsageException(55pht('No log records exist for any daemons.'));56}5758$console = PhutilConsole::getConsole();5960$limit = $args->getArg('limit');6162$logs = id(new PhabricatorDaemonLogEvent())->loadAllWhere(63'logID IN (%Ld) ORDER BY id DESC LIMIT %d',64mpull($daemons, 'getID'),65$limit);66$logs = array_reverse($logs);6768$lines = array();69foreach ($logs as $log) {70$text_lines = phutil_split_lines($log->getMessage(), $retain = false);71foreach ($text_lines as $line) {72$lines[] = array(73'id' => $log->getLogID(),74'type' => $log->getLogType(),75'date' => $log->getEpoch(),76'data' => $line,77);78}79}8081// Each log message may be several lines. Limit the number of lines we82// output so that `--limit 123` means "show 123 lines", which is the most83// easily understandable behavior.84$lines = array_slice($lines, -$limit);8586foreach ($lines as $line) {87$id = $line['id'];88$type = $line['type'];89$data = $line['data'];90$date = date('r', $line['date']);9192$console->writeOut(93"%s\n",94pht(95'Daemon %d %s [%s] %s',96$id,97$type,98$date,99$data));100}101102return 0;103}104105106}107108109