Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
loggerservice.php
1<?php
2
4
8
9final class LoggerService extends BaseService implements ILogger
10{
12 protected static $instance;
13
15 private $logger;
16 private $logLevel;
17 private $eventsToLog;
18
19 public function log(int $level, string $message, int $eventType = 0, array $context = [])
20 {
21 if(!$this->isLevelSatisfied($level) && !$this->isEventSatisfied($eventType))
22 {
23 return;
24 }
25
26 if(count($context) > 0)
27 {
28 $message = $this->interpolate($message, $context);
29 }
30
31 $this->logger->log($level, $message, $eventType, $context);
32 }
33
34 protected function isLevelSatisfied(int $level): bool
35 {
36 return $this->logLevel >= $level;
37 }
38
39 protected function isEventSatisfied(int $eventType): bool
40 {
41 return $eventType > 0 && in_array($eventType, $this->eventsToLog);
42 }
43
44 protected function __construct(Container $config)
45 {
46 parent::__construct($config);
47 $loggerClass = $config->get('logger');
48 $this->logLevel = $config->get('logLevel');
49 $this->eventsToLog = $config->get('eventsToLog');
50 $this->logger = new $loggerClass();
51 }
52
53 private function interpolate($message, array $context = array())
54 {
55 $replace = [];
56
57 foreach ($context as $key => $val)
58 {
59 if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString')))
60 {
61 $replace['{' . $key . '}'] = $val;
62 }
63 }
64
65 return strtr($message, $replace);
66 }
67}
log(int $level, string $message, int $eventType=0, array $context=[])