-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestampedLogger.php
More file actions
58 lines (48 loc) · 1.59 KB
/
Copy pathTimestampedLogger.php
File metadata and controls
58 lines (48 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php declare(strict_types=1);
namespace StellarWP\Foundation\WPCli;
use DateTimeImmutable;
use DateTimeZone;
use WP_CLI\Loggers\Regular;
/**
* WP-CLI logger decorator that prepends timestamps to command output.
*
* Use this when long-running WP-CLI tasks need every line to show when it was
* emitted while preserving WP-CLI's regular logger behavior.
*/
final readonly class TimestampedLogger
{
public function __construct(
private Regular $wpLogger,
private string $dateFormat = 'Y-m-d H:i:s.v e',
private string $timezone = 'UTC'
) {
}
/**
* @param bool|string $group Organize debug messages into a specific group. Use false for no group.
*/
public function debug(string $message, bool|string $group = false): void {
$this->wpLogger->debug($this->prependTimestamp($message), $group);
}
public function info(string $message): void {
$this->wpLogger->info($this->prependTimestamp($message));
}
public function success(string $message): void {
$this->wpLogger->success($this->prependTimestamp($message));
}
public function warning(string $message): void {
$this->wpLogger->warning($this->prependTimestamp($message));
}
/**
* @param list<string> $messageLines Messages to write.
*/
public function error_multi_line(array $messageLines): void {
$this->wpLogger->error_multi_line(array_map([$this, 'prependTimestamp'], $messageLines));
}
private function prependTimestamp(string $message): string {
$timestamp = (new DateTimeImmutable(
'now',
new DateTimeZone($this->timezone)
))->format($this->dateFormat);
return sprintf('[%s] %s', $timestamp, $message);
}
}