Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
requestlogger.php
1<?php
2
4
7use Psr\Log\LoggerInterface;
8use Psr\Log\LogLevel;
9
11{
12 private const OPTION_KEY = 'calendar_logger_enable';
13
17 protected string $serviceName;
18
22 protected int $userId;
23
27 private ?LoggerInterface $logger;
28
34 public static function enable(int $ttl = 0): void
35 {
36 $dateEnd = 0;
37 if ($ttl)
38 {
39 $dateEnd = (time() + $ttl);
40 }
41
42 Option::set('calendar', self::OPTION_KEY, $dateEnd, '-');
43 }
44
48 public static function isEnabled(): bool
49 {
50 $value = Option::get('calendar', self::OPTION_KEY, null, '-');
51 if ($value === null)
52 {
53 return false;
54 }
55
56 $value = (int) $value;
57 if (
58 $value === 0
59 || $value > time()
60 )
61 {
62 return true;
63 }
64
65 return false;
66 }
67
72 public function __construct(int $userId, string $serviceName)
73 {
74 $this->userId = $userId;
75 $this->serviceName = $serviceName;
76 }
77
100 public function write(array $context): void
101 {
102 $context['serviceName'] = $this->serviceName;
103 $context['userId'] = $this->userId;
104 $logger = $this->getLogger();
105 if (is_a($logger, DatabaseLogger::class))
106 {
107 $logger->logToDatabase($context);
108 }
109 else
110 {
111 $logger->log(LogLevel::DEBUG, $this->prepareMessage(), $context);
112 }
113 }
114
115 private function prepareMessage(): string
116 {
117 return "{date} SERVICE_NAME {serviceName}
118 HOST: {host},
119 REQUEST_PARAMS: {requestParams},
120 URL: {url},
121 METHOD: {method},
122 STATUS_CODE: {statusCode},
123 RESPONSE: {response},
124 ERROR: {error}
125 ";
126 }
127
128
132 private function getLogger(): LoggerInterface
133 {
134 if (empty($this->logger))
135 {
136 $this->logger = $this->getDatabaseLogger();
137 }
138 return $this->logger;
139 }
140
141 private function getDatabaseLogger(): DatabaseLogger
142 {
143 return new DatabaseLogger();
144 }
145
146 protected function logMessage(string $level, string $message)
147 {
148
149 }
150}
logMessage(string $level, string $message)
__construct(int $userId, string $serviceName)