1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
LoggerManager.php
См. документацию.
1<?php
2
3namespace Bitrix\Rest\Tools\Diagnostics;
4
5use Bitrix\Main\Config\Option;
6use Bitrix\Main\Diag;
7use Psr\Log\LogLevel;
8
14{
15 public const TYPE_FILE = 'file';
16 public const TYPE_DATABASE = 'db';
17 private const AVAILABLE_TYPE = [
18 self::TYPE_FILE,
19 self::TYPE_DATABASE,
20 ];
21 private const SUPPORTED_LEVEL_LIST = [
22 LogLevel::EMERGENCY,
23 LogLevel::ALERT,
24 LogLevel::CRITICAL,
25 LogLevel::ERROR,
26 LogLevel::WARNING,
27 LogLevel::NOTICE,
28 LogLevel::INFO,
29 LogLevel::DEBUG,
30 ];
31 private const MODULE_ID = 'rest';
32 private const OPTION_LEVEL = 'logger_level';
33 private const OPTION_TYPE = 'logger_type';
34 private const OPTION_FILE_PATH = 'logger_file_path';
35 private const OPTION_LOG_FILTER = 'log_filters';
36 private const OPTION_LOG_END_TIME = 'log_end_time';
37 private const OPTION_SHOW_ARGUMENTS = 'logger_show_args';
38 private const OPTION_SHOW_ARGUMENTS_VALUE_Y = 'Y';
39 private const OPTION_SHOW_ARGUMENTS_VALUE_N = 'N';
40 private static LoggerManager $instance;
41 private bool $isActive;
42 private string $level;
43 private string $type;
44 private ?Diag\Logger $logger = null;
45 private bool $showArguments;
46 private string $path;
47 private ?array $filterOptions = null;
48
49 private function __construct()
50 {
51 $this->isActive = $this->getEndTimeLogging() > time();
52 $this->level = Option::get(self::MODULE_ID,self::OPTION_LEVEL, '');
53 $this->type = Option::get(self::MODULE_ID,self::OPTION_TYPE, self::TYPE_FILE);
54 $this->path = Option::get(self::MODULE_ID,self::OPTION_FILE_PATH, '');
55 $showArguments = Option::get(self::MODULE_ID,self::OPTION_SHOW_ARGUMENTS, self::OPTION_SHOW_ARGUMENTS_VALUE_N);
56 $this->showArguments = $showArguments === self::OPTION_SHOW_ARGUMENTS_VALUE_Y;
57 }
58
59 private function __clone()
60 {
61 }
62
63 public static function getInstance(): LoggerManager
64 {
65 if (!isset(self::$instance))
66 {
67 self::$instance = new LoggerManager;
68 }
69
70 return self::$instance;
71 }
72
73 public function isActive(): bool
74 {
75 return $this->isActive;
76 }
77
78 public function deactivate(): void
79 {
80 $this->isActive = false;
81 Option::delete(self::MODULE_ID, ['name' => self::OPTION_LOG_END_TIME]);
82 }
83
84 public function getLevel(): string
85 {
86 return in_array($this->level, self::SUPPORTED_LEVEL_LIST, true) ? $this->level : '';
87 }
88
89 public function setLevel($level): bool
90 {
91 if (in_array($level, self::SUPPORTED_LEVEL_LIST, true))
92 {
93 $this->level = $level;
94 Option::set(self::MODULE_ID,self::OPTION_LEVEL, $this->level);
95
96 return true;
97 }
98
99 return false;
100 }
101
102 public function getLevelList(): array
103 {
104 return self::SUPPORTED_LEVEL_LIST;
105 }
106
107 public function setType(string $type = self::TYPE_FILE): bool
108 {
109 if (in_array($type, self::AVAILABLE_TYPE, true))
110 {
111 $this->type = $type;
112 Option::set(self::MODULE_ID,self::OPTION_TYPE, $this->type);
113
114 return true;
115 }
116
117 return false;
118 }
119
120 public function getType(): string
121 {
122 return $this->type === self::TYPE_FILE ? self::TYPE_FILE : self::TYPE_DATABASE;
123 }
124
125 public function setFilePath(string $path): bool
126 {
127 if (!empty($path))
128 {
129 $this->path = $path;
130 Option::set(self::MODULE_ID,self::OPTION_FILE_PATH, $this->path);
131
132 return true;
133 }
134
135 return false;
136 }
137
138 public function getFilePath(): string
139 {
140 return $this->path;
141 }
142
143 public function getLogger(): ?Diag\Logger
144 {
145 if (is_null($this->logger) && $this->isActive() && $this->getLevel() !== '')
146 {
147 if ($this->getType() === self::TYPE_FILE)
148 {
149 if (!empty($this->getFilePath()))
150 {
151 $this->logger = new Diag\FileLogger($this->getFilePath());
152 }
153 }
154 else
155 {
156 $this->logger = new DataBaseLogger();
157 }
158
159 if ($this->logger)
160 {
161 $this->logger->setLevel($this->getLevel());
162 $this->logger->setFormatter(
163 new LogFormatter($this->showArguments)
164 );
165 }
166 }
167
168 return $this->logger;
169 }
170
171 public function getFilterOptions(): array
172 {
173 if (!is_array($this->filterOptions))
174 {
175 $options = unserialize(
176 Option::get(self::MODULE_ID, self::OPTION_LOG_FILTER, ''),
177 [
178 'allowed_classes' => false
179 ]
180 );
181 $this->filterOptions = is_array($options) ? $options : [];
182 }
183
185 }
186
187 public function setFilterOptions(array $filters): void
188 {
189 $this->filterOptions = $filters;
190 Option::set(self::MODULE_ID, self::OPTION_LOG_FILTER, serialize($filters));
191 }
192
193 public function getEndTimeLogging(): int
194 {
195 return (int)Option::get(self::MODULE_ID, self::OPTION_LOG_END_TIME, 0);
196 }
197
198 public function setEndTimeLogging(int $time): void
199 {
200 $this->isActive = $time > time();
201 Option::set(self::MODULE_ID, self::OPTION_LOG_END_TIME, $time);
202 }
203}
$path
Определения access_edit.php:21
setFilterOptions(array $filters)
Определения LoggerManager.php:187
setType(string $type=self::TYPE_FILE)
Определения LoggerManager.php:107
$options
Определения commerceml2.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
Определения cachetracker.php:2
$time
Определения payment.php:61
$filterOptions
Определения options.php:63
path
Определения template_copy.php:201