You are here

public function BufferLogger::flush in TMGMT Translator Smartling 8.2

Same name and namespace in other branches
  1. 8.4 src/Logger/BufferLogger.php \Drupal\tmgmt_smartling\Logger\BufferLogger::flush()
  2. 8.3 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 190

Class

BufferLogger
Class BufferLogger

Namespace

Drupal\tmgmt_smartling\Logger

Code

public function flush() {
  if (empty($this->buffer)) {
    return;
  }
  try {
    $records = [];
    $project_id = $this->provider_settings['settings']['project_id'];
    $host = php_uname('n');
    $http_host = $this->request_stack
      ->getCurrentRequest()
      ->getHost();

    // 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->level_translation[$drupal_log_record['level']],
        'channel' => 'drupal-tmgmt-connector_' . $host . '_' . $project_id,
        'datetime' => date('Y-m-d H:i:s', $drupal_log_record['context']['timestamp']),
        'context' => [
          'projectId' => $project_id,
          'host' => $host,
          'http_host' => $http_host,
          'moduleVersion' => SmartlingApiFactory::getModuleVersion(),
          'remoteChannel' => $drupal_log_record['context']['channel'],
        ],
        'message' => strtr($drupal_log_record['message'], $message_placeholders),
      ];
    }
    $this->http_client
      ->request('POST', $this->host, [
      'json' => [
        'records' => $records,
      ],
      'timeout' => $this->time_out,
      'headers' => [
        'User-Agent' => 'drupal-tmgmt-connector/' . SmartlingApiFactory::getModuleVersion(),
      ],
    ]);
  } catch (Exception $e) {
  }
  $this->buffer = [];
}