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