1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class to read monolog config file and instanciate monolog from config |
9
|
|
|
*/ |
10
|
|
|
class Monolog |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @const ERR_HANDLERS_LIST_FORMAT Exception code if config for handlers |
14
|
|
|
* list have not a correct format |
15
|
|
|
*/ |
16
|
|
|
const ERR_HANDLERS_LIST_FORMAT = 1318001; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @const ERR_HANDLER_INFOS_FORMAT Exception code if the handler infos is |
20
|
|
|
* not in a correct format |
21
|
|
|
*/ |
22
|
|
|
const ERR_HANDLER_INFOS_FORMAT = 1318002; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @const ERR_HANDLER_INFOS_MISSING_NAME Exception code if a handler not |
26
|
|
|
* have declared name |
27
|
|
|
*/ |
28
|
|
|
const ERR_HANDLER_INFOS_MISSING_NAME = 1318003; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @const ERR_HANDLER_NAME_NOT_A_STRING Exception code if a handler name |
32
|
|
|
* value is not a string |
33
|
|
|
*/ |
34
|
|
|
const ERR_HANDLER_NAME_NOT_A_STRING = 1318004; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @const ERR_HANDLER_CLASS_NOT_FOUND Exception code if a handler class |
38
|
|
|
* has not been found |
39
|
|
|
*/ |
40
|
|
|
const ERR_HANDLER_CLASS_NOT_FOUND = 1318005; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string $channelName The monolog channel name |
44
|
|
|
*/ |
45
|
|
|
protected $channelName = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \BFW\Config $config The config object containing the handlers list |
49
|
|
|
*/ |
50
|
|
|
protected $config; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Monolog\Logger $monolog The monolog logger object |
54
|
|
|
*/ |
55
|
|
|
protected $monolog; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array $handlers List of all declared handler |
59
|
|
|
*/ |
60
|
|
|
protected $handlers = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Populate properties |
64
|
|
|
* Initialize monolog logger object |
65
|
|
|
* |
66
|
|
|
* @param string $channelName The monolog channel name |
67
|
|
|
* @param \BFW\Config $config The config object containing handlers list |
68
|
|
|
*/ |
69
|
|
|
public function __construct($channelName, \BFW\Config $config) |
70
|
|
|
{ |
71
|
|
|
$this->channelName = (string) $channelName; |
72
|
|
|
$this->config = $config; |
73
|
|
|
$this->monolog = new \Monolog\Logger($this->channelName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get accessor to property channelName |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getChannelName() |
82
|
|
|
{ |
83
|
|
|
return $this->channelName; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get accessor to property config |
88
|
|
|
* |
89
|
|
|
* @return \BFW\Config |
90
|
|
|
*/ |
91
|
|
|
public function getConfig() |
92
|
|
|
{ |
93
|
|
|
return $this->config; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get accessor to property monolog |
98
|
|
|
* |
99
|
|
|
* @return \Monolog\Logger |
100
|
|
|
*/ |
101
|
|
|
public function getMonolog() |
102
|
|
|
{ |
103
|
|
|
return $this->monolog; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get accessor to property handlers |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getHandlers() |
112
|
|
|
{ |
113
|
|
|
return $this->handlers; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Adding all handlers to monolog logger |
118
|
|
|
* |
119
|
|
|
* @param string $configKeyName The key name containing handlers list |
120
|
|
|
* @param string $configFileName The config file name containing handlers |
121
|
|
|
* list |
122
|
|
|
* |
123
|
|
|
* @throws \Exception |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function addAllHandlers( |
128
|
|
|
$configKeyName = 'handlers', |
129
|
|
|
$configFileName = 'monolog.php' |
130
|
|
|
) { |
131
|
|
|
$handlers = $this->config->getValue($configKeyName, $configFileName); |
132
|
|
|
|
133
|
|
|
if (is_object($handlers)) { |
134
|
|
|
$handlers = [$handlers]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (!is_array($handlers)) { |
138
|
|
|
throw new Exception( |
139
|
|
|
'Handlers list into monolog config file should be an array.', |
140
|
|
|
self::ERR_HANDLERS_LIST_FORMAT |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
foreach ($handlers as $handlerInfos) { |
145
|
|
|
$this->addNewHandler($handlerInfos); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check and add a new handler to the logger |
151
|
|
|
* |
152
|
|
|
* @param \stdObject $handlerInfos Handler infos (name and args) |
153
|
|
|
* |
154
|
|
|
* @throws \Exception |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
protected function addNewHandler($handlerInfos) |
159
|
|
|
{ |
160
|
|
|
$this->checkHandlerInfos($handlerInfos); |
161
|
|
|
|
162
|
|
|
$handlerClassName = $handlerInfos->name; |
163
|
|
|
$handler = new $handlerClassName(...$handlerInfos->args); |
164
|
|
|
|
165
|
|
|
$this->handlers[] = $handler; |
166
|
|
|
$this->monolog->pushHandler($handler); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Check the handler infos |
171
|
|
|
* |
172
|
|
|
* @param \stdObject $handlerInfos Handler infos (name and args) |
173
|
|
|
* |
174
|
|
|
* @throws \Exception |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
protected function checkHandlerInfos($handlerInfos) |
179
|
|
|
{ |
180
|
|
|
if (!is_object($handlerInfos)) { |
181
|
|
|
throw new Exception( |
182
|
|
|
'the handler infos should be an object.', |
183
|
|
|
self::ERR_HANDLER_INFOS_FORMAT |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->checkHandlerName($handlerInfos); |
188
|
|
|
$this->checkHandlerArgs($handlerInfos); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Check the handler name |
193
|
|
|
* |
194
|
|
|
* @param \stdObject $handlerInfos Handler infos (name and args) |
195
|
|
|
* |
196
|
|
|
* @throws \Exception |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
protected function checkHandlerName($handlerInfos) |
201
|
|
|
{ |
202
|
|
|
if (!property_exists($handlerInfos, 'name')) { |
203
|
|
|
throw new Exception( |
204
|
|
|
'The handler infos should have the property name', |
205
|
|
|
self::ERR_HANDLER_INFOS_MISSING_NAME |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (!is_string($handlerInfos->name)) { |
210
|
|
|
throw new Exception( |
211
|
|
|
'The handler name should be a string.', |
212
|
|
|
self::ERR_HANDLER_NAME_NOT_A_STRING |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if (!class_exists($handlerInfos->name)) { |
217
|
|
|
throw new Exception( |
218
|
|
|
'The class '.$handlerInfos->name.' has not been found.', |
219
|
|
|
self::ERR_HANDLER_CLASS_NOT_FOUND |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Check the handler arguments list |
226
|
|
|
* |
227
|
|
|
* @param \stdObject $handlerInfos Handler infos (name and args) |
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
|
protected function checkHandlerArgs($handlerInfos) |
232
|
|
|
{ |
233
|
|
|
if (!property_exists($handlerInfos, 'args')) { |
234
|
|
|
$handlerInfos->args = []; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (!is_array($handlerInfos->args)) { |
238
|
|
|
$handlerInfos->args = [$handlerInfos->args]; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|