From bdd873699af367d1cb02ba80b4b955c895c0f50b Mon Sep 17 00:00:00 2001 From: Brian Weaver <87093053+namespacebrian@users.noreply.github.com> Date: Thu, 6 Nov 2025 10:04:47 -0500 Subject: [PATCH] Add timestamps to all logger output messages - Create TimestampedLogOutputStyler that extends LogOutputStyler - Implement timestamp formatting in YYYY-MM-DD hh:mm:ss UTC[+/-offset] format - Configure Terminus to use timestamped logger after Robo container setup - Preserve original log message formatting and colors - Timestamps appear in local timezone with proper UTC offset --- src/Log/TimestampedLogOutputStyler.php | 53 ++++++++++++++++++++++++++ src/Terminus.php | 10 ++++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/Log/TimestampedLogOutputStyler.php diff --git a/src/Log/TimestampedLogOutputStyler.php b/src/Log/TimestampedLogOutputStyler.php new file mode 100644 index 000000000..a7878e1d6 --- /dev/null +++ b/src/Log/TimestampedLogOutputStyler.php @@ -0,0 +1,53 @@ +getTimezone(); + $offset = $now->getOffset(); + + // Format the offset as +/-HHMM + $offsetHours = abs($offset) / 3600; + $offsetMinutes = (abs($offset) % 3600) / 60; + $offsetSign = $offset >= 0 ? '+' : '-'; + $offsetFormatted = sprintf('%s%02d%02d', $offsetSign, $offsetHours, $offsetMinutes); + return $now->format('Y-m-d H:i:s') . ' UTC[' . $offsetFormatted . ']'; + } + + /** + * Apply styling with the provided label and message styles, with timestamp prepended. + * + * @param string $label The log level label + * @param string $message The log message + * @param array $context The log context + * @param string $labelStyle The style for the label + * @param string $messageStyle The style for the message + * @return string The formatted log message with timestamp + */ + protected function formatMessage($label, $message, $context, $labelStyle, $messageStyle = '') + { + // Get the original formatted message with colors + $originalMessage = parent::formatMessage($label, $message, $context, $labelStyle, $messageStyle); + + // Prepend timestamp to the original formatted message + return $this->getTimestamp() . $originalMessage; + } +} diff --git a/src/Terminus.php b/src/Terminus.php index 8936101b7..8ab91e47e 100644 --- a/src/Terminus.php +++ b/src/Terminus.php @@ -132,7 +132,15 @@ public function __construct(Config $config, InputInterface $input, OutputInterfa $this->addDefaultArgumentsAndOptions($application); $this->configureContainer(); Robo::finalizeContainer($this->getContainer()); - $this->setLogger($container->get('logger')); + + // Configure the logger to use timestamps + $logger = $container->get('logger'); + if ($logger instanceof \Consolidation\Log\Logger) { + $timestampedStyler = new \Pantheon\Terminus\Log\TimestampedLogOutputStyler(); + $logger->setLogOutputStyler($timestampedStyler); + } + + $this->setLogger($logger); $this->addBuiltInCommandsAndHooks(); $this->addPluginsCommandsAndHooks();