From 1141405a6befdbee1687826e0a389cd0280f2115 Mon Sep 17 00:00:00 2001 From: MaximeLemolt Date: Thu, 20 Mar 2025 16:16:10 +0100 Subject: [PATCH 1/3] Allow to configuring the date format for console output in watchdog:show command --- src/Commands/core/WatchdogCommands.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Commands/core/WatchdogCommands.php b/src/Commands/core/WatchdogCommands.php index 9cfec17fa2..80041ae15b 100644 --- a/src/Commands/core/WatchdogCommands.php +++ b/src/Commands/core/WatchdogCommands.php @@ -47,6 +47,7 @@ public function __construct(protected Connection $connection) #[CLI\Option(name: 'severity-min', description: 'Restrict to messages of a given severity level and higher.')] #[CLI\Option(name: 'type', description: 'Restrict to messages of a given type.')] #[CLI\Option(name: 'extended', description: 'Return extended information about each message.')] + #[CLI\Option(name: 'date_format', description: 'Specify a date format for the date console output.')] #[CLI\Usage(name: 'drush watchdog:show', description: 'Show a listing of most recent 10 messages.')] #[CLI\Usage(name: 'drush watchdog:show "cron run successful"', description: 'Show a listing of most recent 10 messages containing the string cron run successful.')] #[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) #[CLI\DefaultTableFields(fields: ['wid', 'date', 'type', 'severity', 'message'])] #[CLI\Complete(method_name_or_callable: 'watchdogComplete')] #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] - public function show($substring = '', $options = ['format' => 'table', 'count' => 10, 'severity' => self::REQ, 'severity-min' => self::REQ, 'type' => self::REQ, 'extended' => false]): ?RowsOfFields + 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 { $where = $this->where((string)$options['type'], $options['severity'], $substring, 'AND', $options['severity-min']); $query = $this->connection->select('watchdog', 'w') @@ -82,7 +83,7 @@ public function show($substring = '', $options = ['format' => 'table', 'count' = } $rsc = $query->execute(); while ($result = $rsc->fetchObject()) { - $row = $this->formatResult($result, $options['extended']); + $row = $this->formatResult($result, $options['extended'], $options['date_format']); $table[$row->wid] = (array)$row; } if (empty($table)) { @@ -349,17 +350,19 @@ protected function where(?string $type = null, $severity = null, ?string $filter * A database result object. * @param $extended * Return extended message details. + * @param $date_format + * Specific date format for the date console output. * @return \stdClass * The result object with some attributes themed. */ - protected function formatResult(\stdClass $result, bool $extended = false): \stdClass + protected function formatResult(\stdClass $result, bool $extended = false, string $date_format = 'd/M H:i'): \stdClass { // Severity. $severities = RfcLogLevel::getLevels(); $result->severity = trim(DrupalUtil::drushRender($severities[$result->severity])); // Date. - $result->date = date('d/M H:i', (int)$result->timestamp); + $result->date = date($date_format, (int)$result->timestamp); unset($result->timestamp); // Username. From a7fce11ba0519befbf86a7faab65bc02570d2fbe Mon Sep 17 00:00:00 2001 From: MaximeLemolt Date: Wed, 23 Apr 2025 15:11:06 +0200 Subject: [PATCH 2/3] Fix date format option naming in watchdog:show command --- src/Commands/core/WatchdogCommands.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Commands/core/WatchdogCommands.php b/src/Commands/core/WatchdogCommands.php index 80041ae15b..8165939358 100644 --- a/src/Commands/core/WatchdogCommands.php +++ b/src/Commands/core/WatchdogCommands.php @@ -47,7 +47,7 @@ public function __construct(protected Connection $connection) #[CLI\Option(name: 'severity-min', description: 'Restrict to messages of a given severity level and higher.')] #[CLI\Option(name: 'type', description: 'Restrict to messages of a given type.')] #[CLI\Option(name: 'extended', description: 'Return extended information about each message.')] - #[CLI\Option(name: 'date_format', description: 'Specify a date format for the date console output.')] + #[CLI\Option(name: 'date-format', description: 'Specify a date format for the date console output.')] #[CLI\Usage(name: 'drush watchdog:show', description: 'Show a listing of most recent 10 messages.')] #[CLI\Usage(name: 'drush watchdog:show "cron run successful"', description: 'Show a listing of most recent 10 messages containing the string cron run successful.')] #[CLI\Usage(name: 'drush watchdog:show --count=46', description: 'Show a listing of most recent 46 messages.')] @@ -71,7 +71,7 @@ public function __construct(protected Connection $connection) #[CLI\DefaultTableFields(fields: ['wid', 'date', 'type', 'severity', 'message'])] #[CLI\Complete(method_name_or_callable: 'watchdogComplete')] #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] - 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 + 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 { $where = $this->where((string)$options['type'], $options['severity'], $substring, 'AND', $options['severity-min']); $query = $this->connection->select('watchdog', 'w') @@ -83,7 +83,7 @@ public function show($substring = '', $options = ['format' => 'table', 'count' = } $rsc = $query->execute(); while ($result = $rsc->fetchObject()) { - $row = $this->formatResult($result, $options['extended'], $options['date_format']); + $row = $this->formatResult($result, $options['extended'], $options['date-format']); $table[$row->wid] = (array)$row; } if (empty($table)) { From 9018fc39002fdfe53a5e94d03d62734128b07865 Mon Sep 17 00:00:00 2001 From: MaximeLemolt Date: Wed, 23 Apr 2025 16:54:02 +0200 Subject: [PATCH 3/3] Allow to configuring the date format for console output in watchdog:list and watchdog:show-one commands --- src/Commands/core/WatchdogCommands.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Commands/core/WatchdogCommands.php b/src/Commands/core/WatchdogCommands.php index 8165939358..80cd50a452 100644 --- a/src/Commands/core/WatchdogCommands.php +++ b/src/Commands/core/WatchdogCommands.php @@ -33,6 +33,8 @@ final class WatchdogCommands extends DrushCommands const DELETE = 'watchdog:delete'; const SHOW_ONE = 'watchdog:show-one'; + const DEFAULT_DATE_FORMAT = 'd/M H:i'; + public function __construct(protected Connection $connection) { } @@ -71,7 +73,7 @@ public function __construct(protected Connection $connection) #[CLI\DefaultTableFields(fields: ['wid', 'date', 'type', 'severity', 'message'])] #[CLI\Complete(method_name_or_callable: 'watchdogComplete')] #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] - 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 + public function show($substring = '', $options = ['format' => 'table', 'count' => 10, 'severity' => self::REQ, 'severity-min' => self::REQ, 'type' => self::REQ, 'extended' => false, 'date-format' => self::DEFAULT_DATE_FORMAT]): ?RowsOfFields { $where = $this->where((string)$options['type'], $options['severity'], $substring, 'AND', $options['severity-min']); $query = $this->connection->select('watchdog', 'w') @@ -103,6 +105,7 @@ public function show($substring = '', $options = ['format' => 'table', 'count' = #[CLI\Option(name: 'severity', description: 'Restrict to messages of a given severity level (numeric or string).')] #[CLI\Option(name: 'type', description: 'Restrict to messages of a given type.')] #[CLI\Option(name: 'extended', description: 'Return extended information about each message.')] + #[CLI\Option(name: 'date-format', description: 'Specify a date format for the date console output.')] #[CLI\Usage(name: 'drush watchdog:list', description: 'Prompt for message type or severity, then run watchdog:show.')] #[CLI\FieldLabels(labels: [ 'wid' => 'ID', @@ -119,7 +122,7 @@ public function show($substring = '', $options = ['format' => 'table', 'count' = #[CLI\DefaultTableFields(fields: ['wid', 'date', 'type', 'severity', 'message'])] #[CLI\Complete(method_name_or_callable: 'watchdogComplete')] #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] - public function watchdogList($substring = '', $options = ['format' => 'table', 'count' => 10, 'extended' => false]): ?RowsOfFields + public function watchdogList($substring = '', $options = ['format' => 'table', 'count' => 10, 'extended' => false, 'date-format' => self::DEFAULT_DATE_FORMAT]): ?RowsOfFields { $options['severity-min'] = null; return $this->show($substring, $options); @@ -148,8 +151,8 @@ public function tail(OutputInterface $output, $substring = '', $options = ['seve $where = $this->where($options['type'], $options['severity'], $substring, 'AND', $options['severity-min']); if (empty($where['where'])) { $where = [ - 'where' => 'wid > :wid', - 'args' => [], + 'where' => 'wid > :wid', + 'args' => [], ]; } else { $where['where'] .= " AND wid > :wid"; @@ -258,9 +261,10 @@ public function delete($substring = '', $options = ['severity' => self::REQ, 'ty */ #[CLI\Command(name: self::SHOW_ONE, aliases: ['wd-one', 'watchdog-show-one'])] #[CLI\Argument(name: 'id', description: 'Watchdog Id')] + #[CLI\Option(name: 'date-format', description: 'Specify a date format for the date console output.')] #[CLI\ValidateModulesEnabled(modules: ['dblog'])] #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] - public function showOne($id, $options = ['format' => 'yaml']): PropertyList + public function showOne($id, $options = ['format' => 'yaml', 'date-format' => self::DEFAULT_DATE_FORMAT]): PropertyList { $rsc = $this->connection->select('watchdog', 'w') ->fields('w') @@ -271,7 +275,7 @@ public function showOne($id, $options = ['format' => 'yaml']): PropertyList if (!$result) { throw new \Exception(dt('Watchdog message #!wid not found.', ['!wid' => $id])); } - return new PropertyList($this->formatResult($result, true)); + return new PropertyList($this->formatResult($result, true, $options['date-format'])); } /** @@ -355,7 +359,7 @@ protected function where(?string $type = null, $severity = null, ?string $filter * @return \stdClass * The result object with some attributes themed. */ - protected function formatResult(\stdClass $result, bool $extended = false, string $date_format = 'd/M H:i'): \stdClass + protected function formatResult(\stdClass $result, bool $extended = false, string $date_format = self::DEFAULT_DATE_FORMAT): \stdClass { // Severity. $severities = RfcLogLevel::getLevels();