1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace leinonen\Yii2Monolog; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
use yii\base\Component; |
8
|
|
|
use yii\base\BootstrapInterface; |
9
|
|
|
use leinonen\Yii2Monolog\Factories\MonologFactory; |
10
|
|
|
|
11
|
|
|
class Yii2Monolog extends Component implements BootstrapInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $channels; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var MonologFactory |
20
|
|
|
*/ |
21
|
|
|
private $monologFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $mainChannel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var LoggerRegistry |
30
|
|
|
*/ |
31
|
|
|
private $loggerRegistry; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initiates a new Yii2Monolog. |
35
|
|
|
* |
36
|
|
|
* @param MonologFactory $monologFactory |
37
|
|
|
* @param LoggerRegistry $loggerRegistry |
38
|
|
|
* @param array $config |
39
|
|
|
*/ |
40
|
9 |
|
public function __construct(MonologFactory $monologFactory, LoggerRegistry $loggerRegistry, array $config = []) |
41
|
|
|
{ |
42
|
9 |
|
$this->monologFactory = $monologFactory; |
43
|
9 |
|
$this->loggerRegistry = $loggerRegistry; |
44
|
9 |
|
parent::__construct($config); |
45
|
9 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the given logger channel. |
49
|
|
|
* |
50
|
|
|
* @param null|string $channel |
51
|
|
|
* |
52
|
|
|
* @return Logger |
53
|
|
|
*/ |
54
|
6 |
|
public function getLogger(string $channel = null): Logger |
55
|
|
|
{ |
56
|
6 |
|
if ($channel === null) { |
57
|
1 |
|
$channel = $this->getMainChannel(); |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
return $this->loggerRegistry->getLogger($channel); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
9 |
|
public function bootstrap($app) |
67
|
|
|
{ |
68
|
9 |
|
$this->registerLoggers(); |
69
|
9 |
|
$this->registerPsrLogger(); |
70
|
9 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $channelConfiguration |
74
|
|
|
*/ |
75
|
9 |
|
public function setChannels(array $channelConfiguration) |
76
|
|
|
{ |
77
|
9 |
|
$this->channels = $channelConfiguration; |
78
|
9 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $channelName |
82
|
|
|
*/ |
83
|
2 |
|
public function setMainChannel(string $channelName) |
84
|
|
|
{ |
85
|
2 |
|
$this->mainChannel = $channelName; |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Registers loggers into Yii's DI container. |
90
|
|
|
*/ |
91
|
9 |
|
private function registerLoggers() |
92
|
|
|
{ |
93
|
9 |
|
foreach ($this->channels as $configuredChannelName => $channelConfiguration) { |
94
|
9 |
|
$channelName = $configuredChannelName; |
95
|
9 |
|
$handlers = $channelConfiguration['handlers'] ?? []; |
96
|
9 |
|
$processors = $channelConfiguration['processors'] ?? []; |
97
|
|
|
|
98
|
9 |
|
$this->loggerRegistry->registerLogChannel($channelName, function () use ($channelName, $handlers, $processors) { |
99
|
9 |
|
return $this->monologFactory->make($channelName, $handlers, $processors); |
100
|
9 |
|
}); |
101
|
|
|
} |
102
|
9 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Registers the main channel to be used for Psr LoggerInterface. |
106
|
|
|
*/ |
107
|
|
|
private function registerPsrLogger() |
108
|
|
|
{ |
109
|
9 |
|
$this->loggerRegistry->registerPsrLogger(function () { |
110
|
3 |
|
return $this->getLogger($this->getMainChannel()); |
111
|
9 |
|
}); |
112
|
9 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the main channel to be used for Yii2Monolog component. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
4 |
|
private function getMainChannel(): string |
120
|
|
|
{ |
121
|
4 |
|
if ($this->mainChannel === null) { |
122
|
2 |
|
return array_keys($this->channels)[0]; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return $this->mainChannel; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|