You are here

class NodejsLogger in Node.js integration 8

Sends Nodejs messages for events.

Hierarchy

  • class \Drupal\nodejs_watchdog\Logger\NodejsLogger implements \Psr\Log\LoggerInterface, \Symfony\Component\DependencyInjection\ContainerAwareInterface uses \Symfony\Component\DependencyInjection\ContainerAwareTrait, RfcLoggerTrait, StringTranslationTrait

Expanded class hierarchy of NodejsLogger

1 string reference to 'NodejsLogger'
nodejs_watchdog.services.yml in nodejs_watchdog/nodejs_watchdog.services.yml
nodejs_watchdog/nodejs_watchdog.services.yml
1 service uses NodejsLogger
nodejs_watchdog.mylog in nodejs_watchdog/nodejs_watchdog.services.yml
Drupal\nodejs_watchdog\Logger\NodejsLogger

File

nodejs_watchdog/src/Logger/NodejsLogger.php, line 18

Namespace

Drupal\nodejs_watchdog\Logger
View source
class NodejsLogger implements LoggerInterface, ContainerAwareInterface {
  use RfcLoggerTrait;
  use StringTranslationTrait;
  use ContainerAwareTrait;

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = array()) {
    $classes = [
      RfcLogLevel::DEBUG => 'dblog-debug',
      RfcLogLevel::INFO => 'dblog-info',
      RfcLogLevel::NOTICE => 'dblog-notice',
      RfcLogLevel::WARNING => 'dblog-warning',
      RfcLogLevel::ERROR => 'dblog-error',
      RfcLogLevel::CRITICAL => 'dblog-critical',
      RfcLogLevel::ALERT => 'dblog-alert',
      RfcLogLevel::EMERGENCY => 'dblog-emerg',
    ];

    // Processing and rendering follow the logic in DbLog::log and
    // DbLogController:overview.
    $parser = $this->container
      ->get('logger.log_message_parser');
    $date_formatter = $this->container
      ->get('date.formatter');
    $entity_manager = $this->container
      ->get('entity.manager');
    $user_storage = $entity_manager
      ->getStorage('user');
    $variables = $parser
      ->parseMessagePlaceholders($message, $context);
    $message = $this
      ->t((string) $message, $variables);
    $message = strip_tags($message);
    $username_element = [
      '#theme' => 'username',
      '#account' => $user_storage
        ->load($context['uid']),
    ];
    $row = [
      'data' => [
        [
          'class' => [
            'icon',
          ],
        ],
        $this
          ->t($context['channel']),
        $date_formatter
          ->format($context['timestamp'], 'short'),
        $message,
        [
          'data' => $username_element,
        ],
        $context['link'],
      ],
      // Attributes for table row.
      'class' => [
        Html::getClass('dblog-' . $context['channel']),
        $classes[$level],
      ],
    ];

    // Send ajax command to the fronted to insert the new row into the table
    // on the watchdog page.
    $insert_command = new BeforeCommand('.admin-dblog tr:eq(1)', $this
      ->renderRow($row));
    $commands[] = $insert_command
      ->render();
    $nodejs_message = (object) [
      'channel' => 'watchdog_dblog',
      'commands' => $commands,
      'callback' => 'nodejsWatchdog',
    ];
    nodejs_send_content_channel_message($nodejs_message);
  }

  /**
   * Renders a watchdog row.
   */
  function renderRow($row) {
    $row_attributes = [];
    $output = '';
    $cells = [];
    foreach ($row as $key => $value) {
      if ($key == 'data') {
        $cells = $value;
      }
      else {
        $row_attributes[$key] = $value;
      }
    }
    if (!empty($cells)) {
      $output .= '<tr' . new Attribute($row_attributes) . '>';
      foreach ($cells as $cell) {
        if (is_array($cell)) {
          $data = isset($cell['data']) ? $cell['data'] : '';

          // Cell's data property can be a string or a renderable array.
          if (is_array($data)) {
            $data = $this->container
              ->get('renderer')
              ->renderRoot($data);
          }
          unset($cell['data']);
          $attributes = new Attribute($cell);
        }
        else {
          $attributes = '';
          $data = $cell;
        }
        $output .= '<td' . $attributes . '>' . $data . '</td>';
      }
      $output .= '</tr>';
    }
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodejsLogger::log public function Logs with an arbitrary level. Overrides RfcLoggerTrait::log
NodejsLogger::renderRow function Renders a watchdog row.
RfcLoggerTrait::alert public function
RfcLoggerTrait::critical public function
RfcLoggerTrait::debug public function
RfcLoggerTrait::emergency public function
RfcLoggerTrait::error public function
RfcLoggerTrait::info public function
RfcLoggerTrait::notice public function
RfcLoggerTrait::warning public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.