3 final class PhabricatorDaemonManagementLogWorkflow
4 extends PhabricatorDaemonManagementWorkflow
{
6 protected function didConstruct() {
9 ->setExamples('**log** [__options__]')
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.'))
20 'help' => pht('Show logs for daemon(s) with given ID(s).'),
28 'Show a specific number of log messages (default 100).'),
33 public function execute(PhutilArgumentParser
$args) {
35 $query = id(new PhabricatorDaemonLogQuery())
36 ->setViewer($this->getViewer())
37 ->setAllowStatusWrites(true);
38 $ids = $args->getArg('id');
40 $query->withIDs($ids);
42 $daemons = $query->execute();
43 $daemons = mpull($daemons, null, 'getID');
46 foreach ($ids as $id) {
47 if (!isset($daemons[$id])) {
48 throw new PhutilArgumentUsageException(
50 'No log record exists for a daemon with ID "%s".',
54 } else if (!$daemons) {
55 throw new PhutilArgumentUsageException(
56 pht('No log records exist for any daemons.'));
59 $console = PhutilConsole
::getConsole();
61 $limit = $args->getArg('limit');
63 $logs = id(new PhabricatorDaemonLogEvent())->loadAllWhere(
64 'logID IN (%Ld) ORDER BY id DESC LIMIT %d',
65 mpull($daemons, 'getID'),
67 $logs = array_reverse($logs);
70 foreach ($logs as $log) {
71 $text_lines = phutil_split_lines($log->getMessage(), $retain = false);
72 foreach ($text_lines as $line) {
74 'id' => $log->getLogID(),
75 'type' => $log->getLogType(),
76 'date' => $log->getEpoch(),
82 // Each log message may be several lines. Limit the number of lines we
83 // output so that `--limit 123` means "show 123 lines", which is the most
84 // easily understandable behavior.
85 $lines = array_slice($lines, -$limit);
87 foreach ($lines as $line) {
89 $type = $line['type'];
90 $data = $line['data'];
91 $date = date('r', $line['date']);
96 'Daemon %d %s [%s] %s',