Documentation

Logging

Client supports поддерживает PSR-3 loggers. It's recommended to configure loggers via .settings.php. In this case you can create various loggers instances for each query. This can be useful for logging of couple asynchronous queries - such as for writing debugging into various files and not the mixed portions of various queries into a single file.

Logger constructor gets object type \Bitrix\Main\Web\Http\DebugInterface, allowing to set a logging level (see constants \Bitrix\Main\Web\HttpDebug), and query object \Psr\Http\Message\RequestInterface.

return [
	// ...
	'loggers' => [
		'value' => [
			'main.HttpClient' => [
				'constructor' => function (\Bitrix\Main\Web\Http\DebugInterface $debug, \Psr\Http\Message\RequestInterface $request) { 
					$debug->setDebugLevel(\Bitrix\Main\Web\HttpDebug::ALL);
					return new \Bitrix\Main\Diag\FileLogger('/home/bitrix/www/httplog'. spl_object_hash($request) . '.txt');
				},
				'level' => \Psr\Log\LogLevel::DEBUG,
			],
		],
	],
	// ...
];

Returned logger can have its formatter assigned. For example, you can just log all queries to external resources using http-client, by using the following solutions:

return [
	// ...
	'loggers' => [
		'value' => [
			'main.HttpClient' => [
				'constructor' => function (\Bitrix\Main\Web\Http\DebugInterface $debug, \Psr\Http\Message\RequestInterface $request) {
					$debug->setDebugLevel(\Bitrix\Main\Web\HttpDebug::REQUEST_HEADERS);
	
					$logger = new \Bitrix\Main\Diag\FileLogger($_SERVER['DOCUMENT_ROOT'] . '/http.txt');
	
					$logger->setFormatter(
						new class($request) implements \Bitrix\Main\Diag\LogFormatterInterface 
						{
							public function __construct(public \Psr\Http\Message\RequestInterface $request) {}
	
							public function format($message, array $context = []): string
							{
								// Ignore push server queries
								if ($this->request->getUri()->getPort() === 1337)
								{
									return '';
								}
	
								return $this->request->getUri() . " \t" . $_SERVER['REQUEST_URI'] . "\n";
							}
						}
					);
    
					return $logger;
				},
				'level' => \Psr\Log\LogLevel::DEBUG,
			],
		],
		'readonly' => true,
	],
	// ...
];


© «Bitrix24», 2001-2024