Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
requestlogger.php
1
<?php
2
3
namespace
Bitrix\Calendar\Sync\Util
;
4
5
use
Bitrix\Main
;
6
use
Bitrix\Main\Config\Option
;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LogLevel;
9
10
class
RequestLogger
extends
Main\Diag\Logger
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
}
Bitrix\Calendar\Sync\Util\RequestLogger
Definition
requestlogger.php:11
Bitrix\Calendar\Sync\Util\RequestLogger\logMessage
logMessage(string $level, string $message)
Definition
requestlogger.php:146
Bitrix\Calendar\Sync\Util\RequestLogger\__construct
__construct(int $userId, string $serviceName)
Definition
requestlogger.php:72
Bitrix\Calendar\Sync\Util\RequestLogger\$userId
int $userId
Definition
requestlogger.php:22
Bitrix\Calendar\Sync\Util\RequestLogger\write
write(array $context)
Definition
requestlogger.php:100
Bitrix\Calendar\Sync\Util\RequestLogger\isEnabled
static isEnabled()
Definition
requestlogger.php:48
Bitrix\Calendar\Sync\Util\RequestLogger\enable
static enable(int $ttl=0)
Definition
requestlogger.php:34
Bitrix\Calendar\Sync\Util\RequestLogger\$serviceName
string $serviceName
Definition
requestlogger.php:17
Bitrix\Main\Config\Option
Definition
option.php:15
Bitrix\Main\Diag\Logger
Definition
logger.php:19
Bitrix\Main\Diag\Logger\$message
string $message
Definition
logger.php:37
Bitrix\Main\Diag\Logger\$context
array $context
Definition
logger.php:36
Bitrix\Main\Diag\Logger\$level
$level
Definition
logger.php:31
Bitrix\Calendar\Sync\Util
Definition
attendeesdescription.php:3
Bitrix\Main
modules
calendar
lib
sync
util
requestlogger.php
Создано системой
1.10.0