-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPsr3StackLogger.php
More file actions
115 lines (97 loc) · 2.83 KB
/
Copy pathPsr3StackLogger.php
File metadata and controls
115 lines (97 loc) · 2.83 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
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
<?php declare(strict_types=1);
namespace TimDev\StackLogger;
use Psr\Log\LoggerInterface as PsrInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\NullLogger;
/**
* Implements LoggerInterface by wrapping a PSR3 logger.
*
* @phpstan-template L of PsrInterface
*/
class Psr3StackLogger implements StackLogger
{
use LoggerTrait;
/** @var L */
protected PsrInterface $logger;
/** @var static */
protected ?StackLogger $parent = null;
/** @var array<mixed> */
protected array $context = [];
/** @param L $logger */
public function __construct(PsrInterface $logger)
{
$this->setWrapped($logger);
}
/** @return L */
public function getWrapped(): PsrInterface
{
return $this->logger;
}
/**
* Context Handling
*
* @param array<string, mixed> $context
*/
#[\Override]
public function withContext(array $context = []): static
{
$child = clone $this;
$child->parent = $this;
$child->context = $context;
return $child;
}
/**
* @param array<string, mixed> $context
* @return $this
*/
#[\Override]
public function addContext(array $context): static
{
$this->context = array_merge($this->context, $context);
return $this;
}
/* PSR3 Implementation, wrapping our wrapped instance. */
#[\Override]
public function log($level, string|\Stringable $message, array $context = []): void
{
assert(is_string($level) || is_int($level));
$context = $this->processContext($context);
$this->logger->log($level, $message, $context);
}
public static function makeNullLogger(): StackLogger
{
$instance = new NullLogger();
return new self($instance);
}
/** @param L $logger */
final protected function setWrapped(PsrInterface $logger): void
{
$this->logger = $logger;
}
/**
* Merges $context on top of the instances accumulated context, and
* processes any callable elements in the final context.
*
* @param array<mixed> $context
* @return array<mixed>
*/
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 !== null) ? $this->parent->contextToMerge() : [], $this->context];
}
/** @return array<mixed> */
protected function mergedContext(): array
{
return array_merge(...$this->contextToMerge());
}
}