1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-01 01:18:22 +01:00
phorge-phorge/src/applications/daemon/management/PhabricatorDaemonManagementLogWorkflow.php

93 lines
2.3 KiB
PHP
Raw Normal View History

<?php
final class PhabricatorDaemonManagementLogWorkflow
extends PhabricatorDaemonManagementWorkflow {
public function didConstruct() {
$this
->setName('log')
->setExamples('**log** [__options__]')
->setSynopsis(
pht(
'Print the logs for all daemons, or some daemon(s) identified by '.
'ID. You can get the ID for a daemon from the Daemon Console in '.
'the web interface.'))
->setArguments(
array(
array(
'name' => 'id',
'param' => 'id',
'help' => 'Show logs for daemon(s) with given ID(s).',
'repeat' => true,
),
array(
'name' => 'limit',
'param' => 'N',
'default' => 100,
'help' => 'Show a specific number of log messages '.
'(default 100).',
),
));
}
public function execute(PhutilArgumentParser $args) {
$query = id(new PhabricatorDaemonLogQuery())
->setViewer($this->getViewer())
->setAllowStatusWrites(true);
$ids = $args->getArg('id');
if ($ids) {
$query->withIDs($ids);
}
$daemons = $query->execute();
if (!$daemons) {
if ($ids) {
throw new PhutilArgumentUsageException(
pht('No daemon(s) with id(s) "%s" exist!', implode(', ', $ids)));
} else {
throw new PhutilArgumentUsageException(
pht('No daemons are running.'));
}
}
$console = PhutilConsole::getConsole();
$logs = id(new PhabricatorDaemonLogEvent())->loadAllWhere(
'logID IN (%Ld) ORDER BY id ASC',
mpull($daemons, 'getID'));
$lines = array();
foreach ($logs as $log) {
$text_lines = phutil_split_lines($log->getMessage(), $retain = false);
foreach ($text_lines as $line) {
$lines[] = array(
'id' => $log->getLogID(),
'type' => $log->getLogType(),
'date' => $log->getEpoch(),
'data' => $line,
);
}
}
foreach ($lines as $line) {
$id = $line['id'];
$type = $line['type'];
$data = $line['data'];
$date = date('r', $line['date']);
$console->writeOut(
"%s\n",
sprintf(
'Daemon %d %s [%s] %s',
$id,
$type,
$date,
$data));
}
return 0;
}
}