1 | <?php |
||
36 | class Logger implements ThreadSafeLoggerInterface |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * The thread context of the thread we're logging for. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | public static $threadContext = 'global'; |
||
45 | |||
46 | /** |
||
47 | * The channel name we log to. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $channelName; |
||
52 | |||
53 | /** |
||
54 | * Array with the handlers. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $handlers; |
||
59 | |||
60 | /** |
||
61 | * Array with the processors. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $processors; |
||
66 | |||
67 | /** |
||
68 | * Initializes the logger instance with the log level. |
||
69 | * |
||
70 | * @param string $channelName The channel name |
||
71 | * @param array $handlers The array with the handlers |
||
72 | * @param array $processors The array with the processors |
||
73 | */ |
||
74 | 2 | public function __construct($channelName, array $handlers = array(), array $processors = array()) |
|
75 | { |
||
76 | |||
77 | // initialize the passe values |
||
78 | 2 | $this->channelName = $channelName; |
|
79 | |||
80 | // initialize the stackables |
||
81 | 2 | $this->handlers = array(); |
|
82 | 2 | $this->processors = array(); |
|
83 | |||
84 | // add the passed handlers |
||
85 | 2 | foreach ($handlers as $handler) { |
|
86 | $this->addHandler($handler); |
||
87 | } |
||
88 | |||
89 | // add the passed processors |
||
90 | 2 | foreach ($processors as $processor) { |
|
91 | $this->addProcessor($processor); |
||
92 | } |
||
93 | 2 | } |
|
94 | |||
95 | /** |
||
96 | * Adds the passed handler. |
||
97 | * |
||
98 | * @param \AppserverIo\Logger\Handlers\HandlerInterface $handler The handler to be added |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | 2 | public function addHandler(HandlerInterface $handler) |
|
106 | |||
107 | /** |
||
108 | * Returns the registered handlers. |
||
109 | * |
||
110 | * @return \Stackable The registered handlers |
||
111 | */ |
||
112 | 2 | protected function getHandlers() |
|
116 | |||
117 | /** |
||
118 | * Adds the passed processor. |
||
119 | * |
||
120 | * @param \AppserverIo\Logger\Processors\ProcessorInterface $processor The processor to be added |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | public function addProcessor(ProcessorInterface $processor) |
||
128 | |||
129 | /** |
||
130 | * Returns the registered processors. |
||
131 | * |
||
132 | * @return \Stackable The registered processors |
||
133 | */ |
||
134 | 2 | protected function getProcessors() |
|
138 | |||
139 | /** |
||
140 | * Returns the channel name, passed to the constructor. |
||
141 | * |
||
142 | * @return string The channel name |
||
143 | */ |
||
144 | protected function getChannelName() |
||
148 | |||
149 | /** |
||
150 | * Adds a log record at the DEBUG level. |
||
151 | * |
||
152 | * @param string $message The log message |
||
153 | * @param array $context The log context |
||
154 | * |
||
155 | * @return boolean Whether the record has been processed |
||
156 | */ |
||
157 | public function addDebug($message, array $context = array()) |
||
161 | |||
162 | /** |
||
163 | * Adds a log record at the INFO level. |
||
164 | * |
||
165 | * @param string $message The log message |
||
166 | * @param array $context The log context |
||
167 | * |
||
168 | * @return boolean Whether the record has been processed |
||
169 | */ |
||
170 | public function addInfo($message, array $context = array()) |
||
174 | |||
175 | /** |
||
176 | * Adds a log record at the NOTICE level. |
||
177 | * |
||
178 | * @param string $message The log message |
||
179 | * @param array $context The log context |
||
180 | * |
||
181 | * @return boolean Whether the record has been processed |
||
182 | */ |
||
183 | public function addNotice($message, array $context = array()) |
||
187 | |||
188 | /** |
||
189 | * Adds a log record at the WARNING level. |
||
190 | * |
||
191 | * @param string $message The log message |
||
192 | * @param array $context The log context |
||
193 | * |
||
194 | * @return boolean Whether the record has been processed |
||
195 | */ |
||
196 | public function addWarning($message, array $context = array()) |
||
200 | |||
201 | /** |
||
202 | * Adds a log record at the ERROR level. |
||
203 | * |
||
204 | * @param string $message The log message |
||
205 | * @param array $context The log context |
||
206 | * |
||
207 | * @return boolean Whether the record has been processed |
||
208 | */ |
||
209 | public function addError($message, array $context = array()) |
||
213 | |||
214 | /** |
||
215 | * Adds a log record at the CRITICAL level. |
||
216 | * |
||
217 | * @param string $message The log message |
||
218 | * @param array $context The log context |
||
219 | * |
||
220 | * @return boolean Whether the record has been processed |
||
221 | */ |
||
222 | public function addCritical($message, array $context = array()) |
||
226 | |||
227 | /** |
||
228 | * Adds a log record at the ALERT level. |
||
229 | * |
||
230 | * @param string $message The log message |
||
231 | * @param array $context The log context |
||
232 | * |
||
233 | * @return boolean Whether the record has been processed |
||
234 | */ |
||
235 | public function addAlert($message, array $context = array()) |
||
239 | |||
240 | /** |
||
241 | * Adds a log record at the EMERGENCY level. |
||
242 | * |
||
243 | * @param string $message The log message |
||
244 | * @param array $context The log context |
||
245 | * |
||
246 | * @return boolean Whether the record has been processed |
||
247 | */ |
||
248 | public function addEmergency($message, array $context = array()) |
||
252 | |||
253 | /** |
||
254 | * System is unusable. |
||
255 | * |
||
256 | * @param string $message The message to log |
||
257 | * @param array $context The context for log |
||
258 | * |
||
259 | * @return void |
||
260 | */ |
||
261 | public function emergency($message, array $context = array()) |
||
265 | |||
266 | /** |
||
267 | * Action must be taken immediately. |
||
268 | * |
||
269 | * Example: Entire website down, database unavailable, etc. This should |
||
270 | * trigger the SMS alerts and wake you up. |
||
271 | * |
||
272 | * @param string $message The message to log |
||
273 | * @param array $context The context for log |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | public function alert($message, array $context = array()) |
||
281 | |||
282 | /** |
||
283 | * Critical conditions. |
||
284 | * |
||
285 | * Example: Application component unavailable, unexpected exception. |
||
286 | * |
||
287 | * @param string $message The message to log |
||
288 | * @param array $context The context for log |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | public function critical($message, array $context = array()) |
||
296 | |||
297 | /** |
||
298 | * Runtime errors that do not require immediate action but should typically |
||
299 | * be logged and monitored. |
||
300 | * |
||
301 | * @param string $message The message to log |
||
302 | * @param array $context The context for log |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | public function error($message, array $context = array()) |
||
310 | |||
311 | /** |
||
312 | * Exceptional occurrences that are not errors. |
||
313 | * |
||
314 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
315 | * that are not necessarily wrong. |
||
316 | * |
||
317 | * @param string $message The message to log |
||
318 | * @param array $context The context for log |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | public function warning($message, array $context = array()) |
||
326 | |||
327 | /** |
||
328 | * Normal but significant events. |
||
329 | * |
||
330 | * @param string $message The message to log |
||
331 | * @param array $context The context for log |
||
332 | * |
||
333 | * @return null |
||
334 | */ |
||
335 | public function notice($message, array $context = array()) |
||
339 | |||
340 | /** |
||
341 | * Interesting events. |
||
342 | * |
||
343 | * Example: User logs in, SQL logs. |
||
344 | * |
||
345 | * @param string $message The message to log |
||
346 | * @param array $context The context for log |
||
347 | * |
||
348 | * @return void |
||
349 | */ |
||
350 | 1 | public function info($message, array $context = array()) |
|
354 | |||
355 | /** |
||
356 | * Detailed debug information. |
||
357 | * |
||
358 | * @param string $message The message to log |
||
359 | * @param array $context The context for log |
||
360 | * |
||
361 | * @return void |
||
362 | */ |
||
363 | public function debug($message, array $context = array()) |
||
367 | |||
368 | /** |
||
369 | * Logs with an arbitrary level. |
||
370 | * |
||
371 | * @param mixed $level The log level |
||
372 | * @param string $message The message to log |
||
373 | * @param array $context The context for log |
||
374 | * |
||
375 | * @return void |
||
376 | */ |
||
377 | 2 | public function log($level, $message, array $context = array()) |
|
381 | |||
382 | /** |
||
383 | * Targe for the delegate method of the log message that processes |
||
384 | * the log message asynchronously. |
||
385 | * |
||
386 | * @param \AppserverIo\Logger\LogMessageInterface $logMessage The message to be processed |
||
387 | * |
||
388 | * @return void |
||
389 | */ |
||
390 | 2 | public function process(LogMessageInterface $logMessage) |
|
391 | { |
||
392 | |||
393 | // let the processors process the message |
||
394 | 2 | foreach ($this->getProcessors() as $processor) { |
|
395 | $processor->process($logMessage); |
||
396 | } |
||
397 | |||
398 | // let the handler log the message |
||
399 | 2 | foreach ($this->getHandlers() as $handler) { |
|
400 | 2 | $handler->handle($logMessage); |
|
401 | } |
||
402 | 2 | } |
|
403 | |||
404 | /** |
||
405 | * Appends the passed thread context. |
||
406 | * |
||
407 | * @param string $threadContext The thread context to append |
||
408 | * |
||
409 | * @return void |
||
410 | * @see \AppserverIo\Logger\ThreadSafeLoggerInterface::appendThreadContext() |
||
411 | */ |
||
412 | public function appendThreadContext($threadContext) |
||
416 | } |
||
417 |