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