Skip to content

Commit 1eee273

Browse files
committed
Allow to configuring the date format for console output in watchdog:show command
1 parent 5412e5a commit 1eee273

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/Commands/core/WatchdogCommands.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct(protected Connection $connection)
4747
#[CLI\Option(name: 'severity-min', description: 'Restrict to messages of a given severity level and higher.')]
4848
#[CLI\Option(name: 'type', description: 'Restrict to messages of a given type.')]
4949
#[CLI\Option(name: 'extended', description: 'Return extended information about each message.')]
50+
#[CLI\Option(name: 'date_format', description: 'Specify a date format for the date console output.')]
5051
#[CLI\Usage(name: 'drush watchdog:show', description: 'Show a listing of most recent 10 messages.')]
5152
#[CLI\Usage(name: 'drush watchdog:show "cron run successful"', description: 'Show a listing of most recent 10 messages containing the string <info>cron run successful</info>.')]
5253
#[CLI\Usage(name: 'drush watchdog:show --count=46', description: 'Show a listing of most recent 46 messages.')]
@@ -70,7 +71,7 @@ public function __construct(protected Connection $connection)
7071
#[CLI\DefaultTableFields(fields: ['wid', 'date', 'type', 'severity', 'message'])]
7172
#[CLI\Complete(method_name_or_callable: 'watchdogComplete')]
7273
#[CLI\Bootstrap(level: DrupalBootLevels::FULL)]
73-
public function show($substring = '', $options = ['format' => 'table', 'count' => 10, 'severity' => self::REQ, 'severity-min' => self::REQ, 'type' => self::REQ, 'extended' => false]): ?RowsOfFields
74+
public function show($substring = '', $options = ['format' => 'table', 'count' => 10, 'severity' => self::REQ, 'severity-min' => self::REQ, 'type' => self::REQ, 'extended' => false, 'date_format' => 'd/M H:i']): ?RowsOfFields
7475
{
7576
$where = $this->where((string)$options['type'], $options['severity'], $substring, 'AND', $options['severity-min']);
7677
$query = $this->connection->select('watchdog', 'w')
@@ -82,7 +83,7 @@ public function show($substring = '', $options = ['format' => 'table', 'count' =
8283
}
8384
$rsc = $query->execute();
8485
while ($result = $rsc->fetchObject()) {
85-
$row = $this->formatResult($result, $options['extended']);
86+
$row = $this->formatResult($result, $options['extended'], $options['date_format']);
8687
$table[$row->wid] = (array)$row;
8788
}
8889
if (empty($table)) {
@@ -349,17 +350,19 @@ protected function where(?string $type = null, $severity = null, ?string $filter
349350
* A database result object.
350351
* @param $extended
351352
* Return extended message details.
353+
* @param $date_format
354+
* Specific date format for the date console output.
352355
* @return \stdClass
353356
* The result object with some attributes themed.
354357
*/
355-
protected function formatResult(\stdClass $result, bool $extended = false): \stdClass
358+
protected function formatResult(\stdClass $result, bool $extended = false, string $date_format = 'd/M H:i'): \stdClass
356359
{
357360
// Severity.
358361
$severities = RfcLogLevel::getLevels();
359362
$result->severity = trim(DrupalUtil::drushRender($severities[$result->severity]));
360363

361364
// Date.
362-
$result->date = date('d/M H:i', (int)$result->timestamp);
365+
$result->date = date($date_format, (int)$result->timestamp);
363366
unset($result->timestamp);
364367

365368
// Username.

0 commit comments

Comments
 (0)