Skip to content

Commit b169732

Browse files
feat: Add timestamps to all logger output messages (#2745)
- 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
1 parent 48dd6a5 commit b169732

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pantheon\Terminus\Log;
6+
7+
use Consolidation\Log\LogOutputStyler;
8+
9+
/**
10+
* Extended log output styler that adds timestamps to all log messages.
11+
*
12+
* Formats timestamps as "YYYY-MM-DD hh:mm:ss UTC[+/-offset]" in local timezone.
13+
*/
14+
class TimestampedLogOutputStyler extends LogOutputStyler
15+
{
16+
/**
17+
* Generate a timestamp string in the format "YYYY-MM-DD hh:mm:ss UTC[+/-offset]"
18+
*
19+
* @return string The formatted timestamp
20+
*/
21+
private function getTimestamp(): string
22+
{
23+
$now = new \DateTime();
24+
$timezone = $now->getTimezone();
25+
$offset = $now->getOffset();
26+
27+
// Format the offset as +/-HHMM
28+
$offsetHours = abs($offset) / 3600;
29+
$offsetMinutes = (abs($offset) % 3600) / 60;
30+
$offsetSign = $offset >= 0 ? '+' : '-';
31+
$offsetFormatted = sprintf('%s%02d%02d', $offsetSign, $offsetHours, $offsetMinutes);
32+
return $now->format('Y-m-d H:i:s') . ' UTC[' . $offsetFormatted . ']';
33+
}
34+
35+
/**
36+
* Apply styling with the provided label and message styles, with timestamp prepended.
37+
*
38+
* @param string $label The log level label
39+
* @param string $message The log message
40+
* @param array $context The log context
41+
* @param string $labelStyle The style for the label
42+
* @param string $messageStyle The style for the message
43+
* @return string The formatted log message with timestamp
44+
*/
45+
protected function formatMessage($label, $message, $context, $labelStyle, $messageStyle = '')
46+
{
47+
// Get the original formatted message with colors
48+
$originalMessage = parent::formatMessage($label, $message, $context, $labelStyle, $messageStyle);
49+
50+
// Prepend timestamp to the original formatted message
51+
return $this->getTimestamp() . $originalMessage;
52+
}
53+
}

src/Terminus.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,15 @@ public function __construct(Config $config, InputInterface $input, OutputInterfa
132132
$this->addDefaultArgumentsAndOptions($application);
133133
$this->configureContainer();
134134
Robo::finalizeContainer($this->getContainer());
135-
$this->setLogger($container->get('logger'));
135+
136+
// Configure the logger to use timestamps
137+
$logger = $container->get('logger');
138+
if ($logger instanceof \Consolidation\Log\Logger) {
139+
$timestampedStyler = new \Pantheon\Terminus\Log\TimestampedLogOutputStyler();
140+
$logger->setLogOutputStyler($timestampedStyler);
141+
}
142+
143+
$this->setLogger($logger);
136144
$this->addBuiltInCommandsAndHooks();
137145
$this->addPluginsCommandsAndHooks();
138146

0 commit comments

Comments
 (0)