Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / daemon / management / PhabricatorDaemonManagementLogWorkflow.php
blob8fca624b44a419bea19a779e1babb43bb32b5fa9
1 <?php
3 final class PhabricatorDaemonManagementLogWorkflow
4 extends PhabricatorDaemonManagementWorkflow {
6 protected 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' => pht('Show logs for daemon(s) with given ID(s).'),
21 'repeat' => true,
23 array(
24 'name' => 'limit',
25 'param' => 'N',
26 'default' => 100,
27 'help' => pht(
28 'Show a specific number of log messages (default 100).'),
30 ));
33 public function execute(PhutilArgumentParser $args) {
35 $query = id(new PhabricatorDaemonLogQuery())
36 ->setViewer($this->getViewer())
37 ->setAllowStatusWrites(true);
38 $ids = $args->getArg('id');
39 if ($ids) {
40 $query->withIDs($ids);
42 $daemons = $query->execute();
43 $daemons = mpull($daemons, null, 'getID');
45 if ($ids) {
46 foreach ($ids as $id) {
47 if (!isset($daemons[$id])) {
48 throw new PhutilArgumentUsageException(
49 pht(
50 'No log record exists for a daemon with ID "%s".',
51 $id));
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'),
66 $limit);
67 $logs = array_reverse($logs);
69 $lines = array();
70 foreach ($logs as $log) {
71 $text_lines = phutil_split_lines($log->getMessage(), $retain = false);
72 foreach ($text_lines as $line) {
73 $lines[] = array(
74 'id' => $log->getLogID(),
75 'type' => $log->getLogType(),
76 'date' => $log->getEpoch(),
77 'data' => $line,
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) {
88 $id = $line['id'];
89 $type = $line['type'];
90 $data = $line['data'];
91 $date = date('r', $line['date']);
93 $console->writeOut(
94 "%s\n",
95 pht(
96 'Daemon %d %s [%s] %s',
97 $id,
98 $type,
99 $date,
100 $data));
103 return 0;