You are here

class BufferLogger 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
  2. 8.3 src/Logger/BufferLogger.php \Drupal\tmgmt_smartling\Logger\BufferLogger

Class BufferLogger

@package Drupal\tmgmt_smartling\Logger

Hierarchy

Expanded class hierarchy of BufferLogger

1 string reference to 'BufferLogger'
tmgmt_smartling.services.yml in ./tmgmt_smartling.services.yml
tmgmt_smartling.services.yml
1 service uses BufferLogger
logger.smartling in ./tmgmt_smartling.services.yml
Drupal\tmgmt_smartling\Logger\BufferLogger

File

src/Logger/BufferLogger.php, line 23

Namespace

Drupal\tmgmt_smartling\Logger
View source
class BufferLogger implements LoggerInterface {
  use LoggerTrait;

  /**
   * Map of RFC 5424 log constants to PSR3 log constants.
   *
   * @var array
   */
  protected $level_translation = [
    RfcLogLevel::EMERGENCY => LogLevel::EMERGENCY,
    RfcLogLevel::ALERT => LogLevel::ALERT,
    RfcLogLevel::CRITICAL => LogLevel::CRITICAL,
    RfcLogLevel::ERROR => LogLevel::ERROR,
    RfcLogLevel::WARNING => LogLevel::WARNING,
    RfcLogLevel::NOTICE => LogLevel::NOTICE,
    RfcLogLevel::INFO => LogLevel::INFO,
    RfcLogLevel::DEBUG => LogLevel::DEBUG,
  ];

  /**
   * @var array
   */
  private $buffer;

  /**
   * @var int
   */
  private $buffer_limit;

  /**
   * @var array
   */
  private $channels;

  /**
   * @var int
   */
  private $time_out;

  /**
   * @var LogMessageParserInterface
   */
  private $parser;

  /**
   * @var ConfigFactoryInterface
   */
  private $config_factory;

  /**
   * @var array
   */
  private $provider_settings = [];

  /**
   * @var RequestStack
   */
  private $request_stack;

  /**
   * @var int
   */
  private $log_level;

  /**
   * @var string
   */
  private $host;

  /**
   * @var Client
   */
  private $http_client;

  /**
   * BufferLogger constructor.
   *
   * @param Client $http_client
   * @param LogMessageParserInterface $parser
   * @param ConfigFactoryInterface $config_factory
   * @param RequestStack $request_stack
   * @param array $channels
   * @param int $log_level
   * @param string $host
   * @param int $buffer_limit
   * @param int $time_out
   */
  public function __construct(Client $http_client, LogMessageParserInterface $parser, ConfigFactoryInterface $config_factory, RequestStack $request_stack, $channels = [
    'tmgmt_smartling',
    'tmgmt_smartling_context_debug',
    'tmgmt_extension_suit',
    'smartling_api',
  ], $log_level = RfcLogLevel::DEBUG, $host = 'https://api.smartling.com/updates/status', $buffer_limit = 100, $time_out = 5) {
    $this->buffer = [];
    $this->http_client = $http_client;
    $this->channels = $channels;
    $this->parser = $parser;
    $this->config_factory = $config_factory;
    $this->request_stack = $request_stack;
    $this->log_level = $log_level;
    $this->host = $host;
    $this->buffer_limit = $buffer_limit;
    $this->time_out = $time_out;

    // Pick up first available Smartling provider settings.
    $translator_ids = $this->config_factory
      ->listAll('tmgmt.translator');
    foreach ($translator_ids as $id) {
      $config = $this->config_factory
        ->get($id);
      if ($config
        ->get('plugin') === 'smartling') {
        $this->provider_settings = $config
          ->getRawData();
        break;
      }
    }
    drupal_register_shutdown_function([
      $this,
      'flush',
    ]);
  }

  /**
   * Logs with an arbitrary level.
   *
   * @param mixed $level
   * @param string $message
   * @param array $context
   *
   * @return void
   */
  public function log($level, $message, array $context = []) {

    // Log only records from needed channels.
    if (empty($this->channels) || in_array($context['channel'], $this->channels)) {

      // Key "enable_smartling_logging" might be absent in provider settings
      // after module update. Consider this situation as "checked" by default.
      $enabled_logging = !isset($this->provider_settings['settings']['enable_smartling_logging']) || $this->provider_settings['settings']['enable_smartling_logging'] === TRUE;

      // Buffer log records with $this->log_level or lover.
      if ($level <= $this->log_level && !empty($this->provider_settings['settings']['project_id']) && $enabled_logging) {
        $this->buffer[] = [
          'level' => $level,
          'message' => $message,
          'context' => $context,
        ];

        // Flush buffer on overflow.
        if (count($this->buffer) == $this->buffer_limit) {
          $this
            ->flush();
        }
      }
    }
  }

  /**
   * Log messages into needed destination.
   */
  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 = [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BufferLogger::$buffer private property
BufferLogger::$buffer_limit private property
BufferLogger::$channels private property
BufferLogger::$config_factory private property
BufferLogger::$host private property
BufferLogger::$http_client private property
BufferLogger::$level_translation protected property Map of RFC 5424 log constants to PSR3 log constants.
BufferLogger::$log_level private property
BufferLogger::$parser private property
BufferLogger::$provider_settings private property
BufferLogger::$request_stack private property
BufferLogger::$time_out private property
BufferLogger::flush public function Log messages into needed destination.
BufferLogger::log public function Logs with an arbitrary level. Overrides LoggerTrait::log
BufferLogger::__construct public function BufferLogger constructor.
LoggerTrait::alert public function Action must be taken immediately.
LoggerTrait::critical public function Critical conditions.
LoggerTrait::debug public function Detailed debug information.
LoggerTrait::emergency public function System is unusable.
LoggerTrait::error public function Runtime errors that do not require immediate action but should typically be logged and monitored.
LoggerTrait::info public function Interesting events.
LoggerTrait::notice public function Normal but significant events.
LoggerTrait::warning public function Exceptional occurrences that are not errors.