-
Notifications
You must be signed in to change notification settings - Fork 0
/
Psr3StackLogger.php
153 lines (127 loc) · 3.68 KB
/
Psr3StackLogger.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace TimDev\StackLogger;
use Psr\Log\LoggerInterface as PsrInterface;
use Psr\Log\LogLevel;
/**
* Implements LoggerInterface by wrapping a PSR3 logger.
*
* @template L of PsrInterface
*/
class Psr3StackLogger implements StackLogger
{
/** @var L */
protected PsrInterface $logger;
/** @psalm-var static */
protected ?StackLogger $parent = null;
/** @var array<string, mixed> */
protected array $context = [];
/** @param L $logger */
public function __construct(PsrInterface $logger)
{
$this->setWrapped($logger);
}
/** @return L */
public function getWrapped(): PsrInterface
{
return $this->logger;
}
/** @param L $logger */
final protected function setWrapped(PsrInterface $logger): void
{
$this->logger = $logger;
}
/**
* Context Handling
*
* @param array<string, mixed> $context
* @return static<L>
*/
public function withContext(array $context = []): static
{
$child = clone $this;
$child->parent = $this;
$child->context = $context;
return $child;
}
/**
* @param array<string, mixed> $context
* @return $this
*/
public function addContext(array $context): static
{
$this->context = array_merge($this->context, $context);
return $this;
}
/**
* Merges $context on top of the instances accumulated context, and
* processes any callable elements in the final context.
*/
protected function processContext(array $context = []): array
{
$context = array_merge($this->mergedContext(), $context);
// handle any callables in final context.
return array_map(
static fn($c): mixed => is_callable($c) ? $c($context) : $c,
$context
);
}
/**
* Returns an array like [grandparentContext, parentContext, myContext]
* @return list<array>
*/
protected function contextToMerge(): array
{
return [
... $this->parent ? $this->parent->contextToMerge() : [],
$this->context
];
}
protected function mergedContext(): array
{
return array_merge(...$this->contextToMerge());
}
/* PSR3 Implementation, wrapping our wrapped instance. */
public function log($level, $message, array $context = []): void
{
assert(is_string($level) || is_int($level));
$context = $this->processContext($context);
$this->logger->log($level, $message, $context);
}
public function emergency($message, array $context = []): void
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}
public function alert($message, array $context = [])
{
$this->log(LogLevel::ALERT, $message, $context);
}
public function critical($message, array $context = [])
{
$this->log(LogLevel::CRITICAL, $message, $context);
}
public function error($message, array $context = [])
{
$this->log(LogLevel::ERROR, $message, $context);
}
public function warning($message, array $context = [])
{
$this->log(LogLevel::WARNING, $message, $context);
}
public function notice($message, array $context = [])
{
$this->log(LogLevel::NOTICE, $message, $context);
}
public function info($message, array $context = [])
{
$this->log(LogLevel::INFO, $message, $context);
}
public function debug($message, array $context = [])
{
$this->log(LogLevel::DEBUG, $message, $context);
}
public static function makeNullLogger(): self
{
return new self(new \Psr\Log\NullLogger());
}
}