Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
logger.php
1<?php
2
10namespace Bitrix\Main\Diag;
11
12use Psr\Log;
13use Psr\Log\LogLevel;
17
18abstract class Logger extends Log\AbstractLogger
19{
20 protected static $supportedLevels = [
21 LogLevel::EMERGENCY => LOG_EMERG,
22 LogLevel::ALERT => LOG_ALERT,
23 LogLevel::CRITICAL => LOG_CRIT,
24 LogLevel::ERROR => LOG_ERR,
25 LogLevel::WARNING => LOG_WARNING,
26 LogLevel::NOTICE => LOG_NOTICE,
27 LogLevel::INFO => LOG_INFO,
28 LogLevel::DEBUG => LOG_DEBUG,
29 ];
30
31 protected $level;
32
34 protected $formatter;
35
36 protected array $context;
37 protected string $message;
38
39 protected function interpolate()
40 {
41 if (!isset($this->context['date']))
42 {
43 $this->context['date'] = new Type\DateTime();
44 }
45
46 if (!isset($this->context['host']))
47 {
48 $this->context['host'] = $_SERVER['HTTP_HOST'] ?? '';
49 }
50
51 $formatter = $this->getFormatter();
52
53 return $formatter->format($this->message, $this->context);
54 }
55
59 public function log($level, string|\Stringable $message, array $context = []): void
60 {
61 // Calling this method with a level not defined by this specification MUST throw a Psr\Log\InvalidArgumentException if the implementation does not know about the level.
62 if (!isset(static::$supportedLevels[$level]))
63 {
64 throw new Log\InvalidArgumentException("Log level {$level} is unsupported.");
65 }
66
67 if ($this->level !== null && static::$supportedLevels[$level] > $this->level)
68 {
69 // shouldn't log anything because of the maximum verbose level
70 return;
71 }
72
73 $this->context = $context;
74 $this->message = $message;
75
76 // The message MAY contain placeholders which implementors MAY replace with values from the context array.
77 $message = $this->interpolate();
78
79 if ($message != '')
80 {
81 // actual logging - MUST be defined by a child class
82 $this->logMessage($level, $message);
83 }
84 }
85
86 abstract protected function logMessage(string $level, string $message);
87
93 public function setLevel(string $level)
94 {
95 if (isset(static::$supportedLevels[$level]))
96 {
97 $this->level = static::$supportedLevels[$level];
98 }
99
100 return $this;
101 }
102
109 {
110 $this->formatter = $formatter;
111
112 return $this;
113 }
114
132 public static function create(string $id, $params = [])
133 {
134 $loggersConfig = Config\Configuration::getValue('loggers');
135
136 $logger = null;
137
138 if (isset($loggersConfig[$id]))
139 {
140 $config = $loggersConfig[$id];
141
142 if (isset($config['className']))
143 {
144 $class = $config['className'];
145
146 $args = $config['constructorParams'] ?? [];
147 if ($args instanceof \Closure)
148 {
149 $args = $args();
150 }
151
152 $logger = new $class(...array_values($args));
153 }
154 elseif (isset($config['constructor']))
155 {
156 $closure = $config['constructor'];
157 if ($closure instanceof \Closure)
158 {
159 $logger = $closure(...array_values($params));
160 }
161 }
162
163 if ($logger instanceof static)
164 {
165 if (isset($config['level']))
166 {
167 $logger->setLevel($config['level']);
168 }
169
170 if (isset($config['formatter']))
171 {
172 $serviceLocator = DI\ServiceLocator::getInstance();
173 if ($serviceLocator->has($config['formatter']))
174 {
175 $logger->setFormatter($serviceLocator->get($config['formatter']));
176 }
177 }
178 }
179 }
180
181 return $logger;
182 }
183
184 protected function getFormatter()
185 {
186 if ($this->formatter === null)
187 {
188 $this->formatter = new LogFormatter();
189 }
190
191 return $this->formatter;
192 }
193}
static create(string $id, $params=[])
Definition logger.php:132
setFormatter(LogFormatterInterface $formatter)
Definition logger.php:108
log($level, string|\Stringable $message, array $context=[])
Definition logger.php:59
logMessage(string $level, string $message)
setLevel(string $level)
Definition logger.php:93