Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/daemon/management/PhabricatorDaemonManagementLogWorkflow.php
12256 views
1
<?php
2
3
final class PhabricatorDaemonManagementLogWorkflow
4
extends PhabricatorDaemonManagementWorkflow {
5
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,
22
),
23
array(
24
'name' => 'limit',
25
'param' => 'N',
26
'default' => 100,
27
'help' => pht(
28
'Show a specific number of log messages (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
$daemons = mpull($daemons, null, 'getID');
44
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));
52
}
53
}
54
} else if (!$daemons) {
55
throw new PhutilArgumentUsageException(
56
pht('No log records exist for any daemons.'));
57
}
58
59
$console = PhutilConsole::getConsole();
60
61
$limit = $args->getArg('limit');
62
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);
68
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,
78
);
79
}
80
}
81
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);
86
87
foreach ($lines as $line) {
88
$id = $line['id'];
89
$type = $line['type'];
90
$data = $line['data'];
91
$date = date('r', $line['date']);
92
93
$console->writeOut(
94
"%s\n",
95
pht(
96
'Daemon %d %s [%s] %s',
97
$id,
98
$type,
99
$date,
100
$data));
101
}
102
103
return 0;
104
}
105
106
107
}
108
109