1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
logger.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Diag;
11
12use Psr\Log;
13use Psr\Log\LogLevel;
14use Bitrix\Main\Config;
15use Bitrix\Main\DI;
16
17abstract class Logger extends Log\AbstractLogger
18{
19 protected static $supportedLevels = [
20 LogLevel::EMERGENCY => LOG_EMERG,
21 LogLevel::ALERT => LOG_ALERT,
22 LogLevel::CRITICAL => LOG_CRIT,
23 LogLevel::ERROR => LOG_ERR,
24 LogLevel::WARNING => LOG_WARNING,
25 LogLevel::NOTICE => LOG_NOTICE,
26 LogLevel::INFO => LOG_INFO,
27 LogLevel::DEBUG => LOG_DEBUG,
28 ];
29
30 protected $level;
31
33 protected $formatter;
34
35 protected array $context;
36 protected string $message;
37
38 protected function interpolate()
39 {
40 $formatter = $this->getFormatter();
41
42 return $formatter->format($this->message, $this->context);
43 }
44
48 public function log($level, string|\Stringable $message, array $context = []): void
49 {
50 // 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.
51 if (!isset(static::$supportedLevels[$level]))
52 {
53 throw new Log\InvalidArgumentException("Log level {$level} is unsupported.");
54 }
55
56 if ($this->level !== null && static::$supportedLevels[$level] > $this->level)
57 {
58 // shouldn't log anything because of the maximum verbose level
59 return;
60 }
61
62 $this->context = $context;
63 $this->message = $message;
64
65 // The message MAY contain placeholders which implementors MAY replace with values from the context array.
66 $message = $this->interpolate();
67
68 if ($message != '')
69 {
70 // actual logging - MUST be defined by a child class
71 $this->logMessage($level, $message);
72 }
73 }
74
75 abstract protected function logMessage(string $level, string $message);
76
82 public function setLevel(string $level)
83 {
84 if (isset(static::$supportedLevels[$level]))
85 {
86 $this->level = static::$supportedLevels[$level];
87 }
88
89 return $this;
90 }
91
98 {
99 $this->formatter = $formatter;
100
101 return $this;
102 }
103
121 public static function create(string $id, $params = [])
122 {
123 $loggersConfig = Config\Configuration::getValue('loggers');
124
125 $logger = null;
126
127 if (isset($loggersConfig[$id]))
128 {
129 $config = $loggersConfig[$id];
130
131 if (isset($config['className']))
132 {
133 $class = $config['className'];
134
135 $args = $config['constructorParams'] ?? [];
136 if ($args instanceof \Closure)
137 {
138 $args = $args();
139 }
140
141 $logger = new $class(...array_values($args));
142 }
143 elseif (isset($config['constructor']))
144 {
145 $closure = $config['constructor'];
146 if ($closure instanceof \Closure)
147 {
148 $logger = $closure(...array_values($params));
149 }
150 }
151
152 if ($logger instanceof static)
153 {
154 if (isset($config['level']))
155 {
156 $logger->setLevel($config['level']);
157 }
158
159 if (isset($config['formatter']))
160 {
161 $serviceLocator = DI\ServiceLocator::getInstance();
162 if ($serviceLocator->has($config['formatter']))
163 {
164 $logger->setFormatter($serviceLocator->get($config['formatter']));
165 }
166 }
167 }
168 }
169
170 return $logger;
171 }
172
173 protected function getFormatter()
174 {
175 if ($this->formatter === null)
176 {
177 $this->formatter = new LogFormatter();
178 }
179
180 return $this->formatter;
181 }
182}
static getValue($name)
Определения configuration.php:24
static getInstance()
Определения servicelocator.php:33
string $message
Определения logger.php:36
static create(string $id, $params=[])
Определения logger.php:121
array $context
Определения logger.php:35
setFormatter(LogFormatterInterface $formatter)
Определения logger.php:97
log($level, string|\Stringable $message, array $context=[])
Определения logger.php:48
logMessage(string $level, string $message)
interpolate()
Определения logger.php:38
$formatter
Определения logger.php:33
getFormatter()
Определения logger.php:173
$level
Определения logger.php:30
static $supportedLevels
Определения logger.php:19
setLevel(string $level)
Определения logger.php:82
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799