Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
LoggerManager.php
1<?php
2
4
7use Psr\Log\LogLevel;
8
14{
15 public const TYPE_FILE = 'file';
16 public const TYPE_DATABASE = 'db';
17 private const AVAILABLE_TYPE = [
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_ACTIVE = 'logger_active';
36 private const OPTION_ACTIVE_VALUE_Y = 'Y';
37 private const OPTION_ACTIVE_VALUE_N = 'N';
38 private const OPTION_SHOW_ARGUMENTS = 'logger_show_args';
39 private const OPTION_SHOW_ARGUMENTS_VALUE_Y = 'Y';
40 private const OPTION_SHOW_ARGUMENTS_VALUE_N = 'N';
41
43 private static $instance;
44
45 private $isActive;
46 private $level;
47 private $type;
48 private $logger;
49 private $showArguments;
50 private $path;
51
52 private function __construct()
53 {
54 $active = Option::get(self::MODULE_ID,self::OPTION_ACTIVE, self::OPTION_ACTIVE_VALUE_N);
55 $this->isActive = $active === self::OPTION_ACTIVE_VALUE_Y;
56 $this->level = Option::get(self::MODULE_ID,self::OPTION_LEVEL, '');
57 $this->type = Option::get(self::MODULE_ID,self::OPTION_TYPE, self::TYPE_FILE);
58 $this->path = Option::get(self::MODULE_ID,self::OPTION_FILE_PATH, '');
59 $showArguments = Option::get(self::MODULE_ID,self::OPTION_SHOW_ARGUMENTS, self::OPTION_SHOW_ARGUMENTS_VALUE_N);
60 $this->showArguments = $showArguments === self::OPTION_SHOW_ARGUMENTS_VALUE_Y;
61 }
62
63 private function __clone()
64 {
65 }
66
72 public static function getInstance(): LoggerManager
73 {
74 if (self::$instance === null)
75 {
76 self::$instance = new LoggerManager;
77 }
78
79 return self::$instance;
80 }
81
86 public function isActive(): bool
87 {
88 return $this->isActive;
89 }
90
98 public function setActive(bool $active = false): bool
99 {
100 $this->isActive = $active;
101 Option::set(
102 self::MODULE_ID,
103 self::OPTION_ACTIVE,
104 $active
105 ? self::OPTION_ACTIVE_VALUE_Y
106 : self::OPTION_ACTIVE_VALUE_N
107 );
108
109 return true;
110 }
111
119 public function setShowArguments(bool $show = false): bool
120 {
121 $this->showArguments = $show;
122 Option::set(
123 self::MODULE_ID,
124 self::OPTION_ACTIVE,
125 $this->showArguments
126 ? self::OPTION_SHOW_ARGUMENTS_VALUE_Y
127 : self::OPTION_SHOW_ARGUMENTS_VALUE_N
128 );
129
130 return true;
131 }
132
138 public function getLevel(): string
139 {
140 return in_array($this->level, self::SUPPORTED_LEVEL_LIST, true) ? $this->level : '';
141 }
142
150 public function setLevel($level): bool
151 {
152 if (in_array($level, self::SUPPORTED_LEVEL_LIST, true))
153 {
154 $this->level = $level;
155 Option::set(self::MODULE_ID,self::OPTION_LEVEL, $this->level);
156
157 return true;
158 }
159
160 return false;
161 }
162
170 public function setType($type = self::TYPE_FILE): bool
171 {
172 if (in_array($type, self::AVAILABLE_TYPE, true))
173 {
174 $this->type = $type;
175 Option::set(self::MODULE_ID,self::OPTION_TYPE, $this->type);
176
177 return true;
178 }
179
180 return false;
181 }
182
187 public function getType()
188 {
189 return $this->type === self::TYPE_FILE ? self::TYPE_FILE : self::TYPE_DATABASE;
190 }
191
199 public function setFilePath(string $path): bool
200 {
201 if (!empty($path))
202 {
203 $this->path = $path;
204 Option::set(self::MODULE_ID,self::OPTION_FILE_PATH, $this->path);
205
206 return true;
207 }
208
209 return false;
210 }
211
217 public function getFilePath(): string
218 {
219 return $this->path;
220 }
221
228 public function getLogger(array $params = []): ?Diag\Logger
229 {
230 if (!$this->logger && $this->isActive() && $this->getLevel() !== '')
231 {
232 if ($this->getType() === self::TYPE_FILE)
233 {
234 if (!empty($this->getFilePath()))
235 {
236 $this->logger = new Diag\FileLogger($this->getFilePath());
237 }
238 }
239 else
240 {
241 $this->logger = new DataBaseLogger();
242 }
243
244 if ($this->logger)
245 {
246 $this->logger->setLevel($this->getLevel());
247 $this->logger->setFormatter(
248 new Diag\LogFormatter($this->showArguments)
249 );
250 }
251 }
252
253 return $this->logger;
254 }
255}