You are here

class LoggerChannel in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 modules/tmgmt_smartling_log_settings/src/Logger/LoggerChannel.php \Drupal\tmgmt_smartling_log_settings\Logger\LoggerChannel

Defines a logger channel that most implementations will use.

Same as core's LoggerChannel but with "suppress logging" feature.

Hierarchy

Expanded class hierarchy of LoggerChannel

File

modules/tmgmt_smartling_log_settings/src/Logger/LoggerChannel.php, line 14

Namespace

Drupal\tmgmt_smartling_log_settings\Logger
View source
class LoggerChannel extends LoggerChannelCore {

  /**
   * @var array|mixed
   */
  private $severity_mapping;

  /**
   * {@inheritdoc}
   */
  public function __construct($channel) {
    $severity_mapping =& drupal_static(get_called_class());
    if (!isset($severity_mapping)) {
      $config = \Drupal::configFactory()
        ->getEditable('tmgmt_smartling_log_settings.settings')
        ->get('severity_mapping');
      $severity_mapping = Yaml::decode($config);
    }
    $this->severity_mapping = $severity_mapping;
    parent::__construct($channel);
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    if ($this->callDepth == self::MAX_CALL_DEPTH) {
      return;
    }
    $this->callDepth++;

    // Merge in defaults.
    $context += [
      'channel' => $this->channel,
      'link' => '',
      'user' => NULL,
      'uid' => 0,
      'request_uri' => '',
      'referer' => '',
      'ip' => '',
      'timestamp' => time(),
    ];

    // Some context values are only available when in a request context.
    if ($this->requestStack && ($request = $this->requestStack
      ->getCurrentRequest())) {
      $context['request_uri'] = $request
        ->getUri();
      $context['referer'] = $request->headers
        ->get('Referer', '');
      $context['ip'] = $request
        ->getClientIP();
      try {
        if ($this->currentUser) {
          $context['user'] = $this->currentUser;
          $context['uid'] = $this->currentUser
            ->id();
        }
      } catch (\Exception $e) {

        // An exception might be thrown if the database connection is not
        // available or due to another unexpected reason. It is more important
        // to log the error that we already have so any additional exceptions
        // are ignored.
      }
    }
    if (is_string($level)) {

      // Convert to integer equivalent for consistency with RFC 5424.
      $level = $this->levelTranslation[$level];
    }

    // Call all available loggers.
    foreach ($this
      ->sortLoggers() as $logger) {
      $logger_class = get_class($logger);

      // Log records into Smartling anyway. BufferLogger is subscribed ONLY
      // to Smartling related channels (smartling_api, tmgmt_smartling and
      // tmgmt_extension_suit). On/Off logic happens inside BufferLogger::log().
      if ($logger_class == 'Drupal\\tmgmt_smartling\\Logger\\BufferLogger') {
        $logger
          ->log($level, $message, $context);
      }
      else {
        $config_level = !empty($this->severity_mapping[$this->channel]) ? $this->levelTranslation[$this->severity_mapping[$this->channel]] : RfcLogLevel::DEBUG;
        if (!empty($this->severity_mapping[$this->channel])) {
          if ($level <= $config_level) {
            $logger
              ->log($level, $message, $context);
          }
        }
        else {
          $logger
            ->log($level, $message, $context);
        }
      }
    }
    $this->callDepth--;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerChannel::$callDepth protected property Number of times LoggerChannel::log() has been called for a single message.
LoggerChannel::$channel protected property The name of the channel of this logger instance.
LoggerChannel::$currentUser protected property The current user object.
LoggerChannel::$levelTranslation protected property Map of PSR3 log constants to RFC 5424 log constants.
LoggerChannel::$loggers protected property An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
LoggerChannel::$requestStack protected property The request stack object.
LoggerChannel::$severity_mapping private property
LoggerChannel::addLogger public function Adds a logger. Overrides LoggerChannelInterface::addLogger
LoggerChannel::log public function Logs with an arbitrary level. Overrides LoggerChannel::log
LoggerChannel::MAX_CALL_DEPTH constant Maximum call depth to self::log() for a single log message.
LoggerChannel::setCurrentUser public function Sets the current user. Overrides LoggerChannelInterface::setCurrentUser
LoggerChannel::setLoggers public function Sets the loggers for this channel. Overrides LoggerChannelInterface::setLoggers
LoggerChannel::setRequestStack public function Sets the request stack. Overrides LoggerChannelInterface::setRequestStack
LoggerChannel::sortLoggers protected function Sorts loggers according to priority.
LoggerChannel::__construct public function Constructs a LoggerChannel object Overrides LoggerChannel::__construct