1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Monolog; |
6
|
|
|
|
7
|
|
|
use Monolog\Handler\HandlerInterface; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
use Monolog\Processor\PsrLogMessageProcessor; |
10
|
|
|
use Monolog\ResettableInterface; |
11
|
|
|
use Psr\Container\ContainerExceptionInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Spiral\Core\Container\Autowire; |
14
|
|
|
use Spiral\Core\Container\InjectorInterface; |
15
|
|
|
use Spiral\Core\FactoryInterface; |
16
|
|
|
use Spiral\Logger\ListenerRegistryInterface; |
17
|
|
|
use Spiral\Logger\LoggerInjector; |
18
|
|
|
use Spiral\Logger\LogsInterface; |
19
|
|
|
use Spiral\Monolog\Config\MonologConfig; |
20
|
|
|
use Spiral\Monolog\Exception\ConfigException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @implements InjectorInterface<Logger> |
24
|
|
|
*/ |
25
|
|
|
final class LogFactory implements LogsInterface, InjectorInterface, ResettableInterface |
26
|
|
|
{ |
27
|
|
|
private ?LoggerInterface $default = null; |
28
|
|
|
private readonly HandlerInterface $eventHandler; |
29
|
|
|
|
30
|
22 |
|
public function __construct( |
31
|
|
|
private readonly MonologConfig $config, |
32
|
|
|
ListenerRegistryInterface $listenerRegistry, |
33
|
|
|
private readonly FactoryInterface $factory |
34
|
|
|
) { |
35
|
22 |
|
$this->eventHandler = new EventHandler($listenerRegistry, $config->getEventLevel()); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
22 |
|
public function getLogger(?string $channel = null): LoggerInterface |
39
|
|
|
{ |
40
|
22 |
|
$default = $this->config->getDefault(); |
41
|
|
|
|
42
|
22 |
|
if ($channel === null || $channel === $default) { |
43
|
7 |
|
if ($this->default !== null) { |
44
|
|
|
// we should use only one default logger per system |
45
|
4 |
|
return $this->default; |
46
|
|
|
} |
47
|
|
|
|
48
|
7 |
|
return $this->default = new Logger( |
49
|
7 |
|
$default, |
50
|
7 |
|
$this->getHandlers($default), |
51
|
7 |
|
$this->getProcessors($default) |
52
|
7 |
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
17 |
|
return new Logger( |
56
|
17 |
|
$channel, |
57
|
17 |
|
$this->getHandlers($channel), |
58
|
17 |
|
$this->getProcessors($channel) |
59
|
17 |
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @deprecated use {@see LoggerInjector} as an injector instead. |
64
|
|
|
*/ |
65
|
|
|
public function createInjection(\ReflectionClass $class, ?string $context = null): LoggerInterface |
66
|
|
|
{ |
67
|
|
|
return $this->getLogger(); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function reset(): void |
71
|
|
|
{ |
72
|
1 |
|
if ($this->default instanceof ResettableInterface) { |
73
|
1 |
|
$this->default->reset(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get list of channel specific handlers. |
79
|
|
|
* |
80
|
|
|
* |
81
|
|
|
* @throws ConfigException |
82
|
|
|
*/ |
83
|
22 |
|
protected function getHandlers(string $channel): array |
84
|
|
|
{ |
85
|
|
|
// always include default handler |
86
|
22 |
|
$handlers = []; |
87
|
|
|
|
88
|
22 |
|
foreach ($this->config->getHandlers($channel) as $handler) { |
89
|
6 |
|
if (!$handler instanceof Autowire) { |
90
|
2 |
|
$handlers[] = $handler; |
91
|
2 |
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
try { |
95
|
4 |
|
$handlers[] = $handler->resolve($this->factory); |
96
|
|
|
} catch (ContainerExceptionInterface $e) { |
97
|
|
|
throw new ConfigException($e->getMessage(), $e->getCode(), $e); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
21 |
|
$handlers[] = $this->eventHandler; |
102
|
|
|
|
103
|
21 |
|
return $handlers; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get list of channel specific log processors. |
108
|
|
|
* |
109
|
|
|
* @return callable[] |
110
|
|
|
*/ |
111
|
21 |
|
protected function getProcessors(string $channel): array |
112
|
|
|
{ |
113
|
21 |
|
$processors = []; |
114
|
21 |
|
foreach ($this->config->getProcessors($channel) as $processor) { |
115
|
6 |
|
if (!$processor instanceof Autowire) { |
116
|
3 |
|
$processors[] = $processor; |
117
|
3 |
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
try { |
121
|
3 |
|
$processors[] = $processor->resolve($this->factory); |
122
|
|
|
} catch (ContainerExceptionInterface $e) { |
123
|
|
|
throw new ConfigException($e->getMessage(), $e->getCode(), $e); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
20 |
|
if ($processors === []) { |
128
|
14 |
|
$processors[] = new PsrLogMessageProcessor(); |
129
|
|
|
} |
130
|
|
|
|
131
|
20 |
|
return $processors; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|