You are here

public function BufferLogger::flush in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 src/Logger/BufferLogger.php \Drupal\tmgmt_smartling\Logger\BufferLogger::flush()
  2. 8.2 src/Logger/BufferLogger.php \Drupal\tmgmt_smartling\Logger\BufferLogger::flush()

Log messages into needed destination.

1 call to BufferLogger::flush()
BufferLogger::log in src/Logger/BufferLogger.php
Logs with an arbitrary level.

File

src/Logger/BufferLogger.php, line 203

Class

BufferLogger
Class BufferLogger

Namespace

Drupal\tmgmt_smartling\Logger

Code

public function flush() {
  if (empty($this->buffer)) {
    return;
  }
  try {
    $records = [];
    $project_id = $this->providerSettings['settings']['project_id'];
    $host = php_uname('n');
    $http_host = $this->requestStack
      ->getCurrentRequest()
      ->getHost();
    $tmgmt_smartling_version = ConnectorInfo::getLibVersion();
    $dependencies = ConnectorInfo::getDependenciesVersionsAsString();

    // Assemble records.
    foreach ($this->buffer as $drupal_log_record) {
      $message_placeholders = $this->parser
        ->parseMessagePlaceholders($drupal_log_record['message'], $drupal_log_record['context']);
      $records[] = [
        'level_name' => $this->levelTranslation[$drupal_log_record['level']],
        'channel' => 'drupal-tmgmt-connector',
        'datetime' => date('Y-m-d H:i:s', $drupal_log_record['context']['timestamp']),
        'context' => [
          'projectId' => $project_id,
          'host' => $host,
          'http_host' => $http_host,
          'moduleVersion' => $tmgmt_smartling_version,
          'dependencies' => $dependencies,
          'remoteChannel' => $drupal_log_record['context']['channel'],
          'requestId' => $this->uid,
        ],
        'message' => strtr($drupal_log_record['message'], $message_placeholders),
      ];
    }
    $this->httpClient
      ->request('POST', $this->host, [
      'json' => [
        'records' => $records,
      ],
      'timeout' => $this->timeOut,
      'headers' => [
        'User-Agent' => ConnectorInfo::getLibName() . '/' . ConnectorInfo::getLibVersion(),
      ],
    ]);
  } catch (Exception $e) {
  }
  $this->buffer = [];
}