SolariumRequestLogger.php in Search API Solr 8.3
File
modules/search_api_solr_devel/src/Logging/SolariumRequestLogger.php
View source
<?php
namespace Drupal\search_api_solr_devel\Logging;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\devel\DevelDumperManagerInterface;
use Drupal\search_api\LoggerTrait;
use Drupal\search_api_solr\Utility\Utility;
use Solarium\Core\Event\Events;
use Solarium\Core\Event\PreExecuteRequest;
use Solarium\Core\Event\PostExecuteRequest;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SolariumRequestLogger implements EventSubscriberInterface {
use StringTranslationTrait;
use LoggerTrait;
protected $develDumperManager;
public function __construct(DevelDumperManagerInterface $develDumperManager) {
$this->develDumperManager = $develDumperManager;
}
public static function getSubscribedEvents() {
return [
Events::PRE_EXECUTE_REQUEST => 'preExecuteRequest',
Events::POST_EXECUTE_REQUEST => 'postExecuteRequest',
];
}
public function preExecuteRequest(PreExecuteRequest $event) {
$request = $event
->getRequest();
$parsedRequestParams = Utility::parseRequestParams($request);
$this->develDumperManager
->message($request
->getUri(), $this
->t('Try to send Solr request'));
$this->develDumperManager
->message($parsedRequestParams, $request
->getMethod());
$this
->getLogger()
->debug($request
->getQueryString());
}
public function postExecuteRequest(PostExecuteRequest $event) {
$response = $event
->getResponse();
$this->develDumperManager
->message($response
->getStatusCode() . ' ' . $response
->getStatusMessage(), $this
->t('Received Solr response'));
$this
->getLogger()
->debug('@solr_request_body', [
'@solr_request_body' => $response
->getBody(),
]);
$this
->showLoggerHint();
}
protected function showLoggerHint() {
static $hint = FALSE;
if (!$hint) {
$hint = TRUE;
$this->develDumperManager
->message('Type: search_api, Severity: Debug', $this
->t('Check the logs for detailed Solr response bodies'), 'warning');
}
}
}