You are here

class Logger in MongoDB 8

MongoDB logger implementation for watchdog().

@package Drupal\mongodb\Logger

Hierarchy

  • class \Drupal\mongodb\Logger\Logger extends \Psr\Log\AbstractLogger

Expanded class hierarchy of Logger

2 files declare their use of Logger
LoggerClearForm.php in src/Form/LoggerClearForm.php
Contains \Drupal\mongodb\Logger\LoggerClearForm.php
LoggerFilterForm.php in src/Form/LoggerFilterForm.php
Contains \Drupal\mongodb\Form\DblogFilterForm.
1 string reference to 'Logger'
mongodb_watchdog.services.yml in mongodb_watchdog/mongodb_watchdog.services.yml
mongodb_watchdog/mongodb_watchdog.services.yml
1 service uses Logger
mongo.logger in mongodb_watchdog/mongodb_watchdog.services.yml
Drupal\mongodb\Logger\Logger

File

src/Logger/Logger.php, line 19
Logger functionality (watchdog).

Namespace

Drupal\mongodb\Logger
View source
class Logger extends AbstractLogger {
  const TEMPLATE_COLLECTION = 'watchdog';
  const EVENT_COLLECTION_PREFIX = 'watchdog_event_';
  const EVENT_COLLECTIONS_PATTERN = '/watchdog_event_[[:xdigit:]]{32}$/';

  /**
   * @var \Drupal\mongodb\MongoCollectionFactory
   */
  protected $collection_factory;

  /**
   * @var \MongoCollection
   */
  protected $message_templates;

  /**
   * @param \Drupal\mongodb\MongoCollectionFactory $collection_factory
   */
  public function __construct(MongoCollectionFactory $collection_factory) {
    $this->collection_factory = $collection_factory;
    $this->message_templates = $collection_factory
      ->get(static::TEMPLATE_COLLECTION);
  }

  /**
   * TODO implement this method to replace the code in mongodb_watchdog().
   *
   * @param mixed $level
   * @param string $message
   * @param array $context
   *
   * @return null|void
   */
  public function log($level, $message, array $context = array()) {
    echo "Logging {$level}";
    var_dump($message);
  }

  /**
   * @return \MongoCollection[]
   *
   */
  public function listCollections() {
    return $this
      ->templatesCollection()->db
      ->listCollections();
  }

  /**
   * @param $template_id
   *
   * @return \MongoCollection
   */
  public function eventCollection($template_id) {
    $collection_name = static::EVENT_COLLECTION_PREFIX . $template_id;
    assert('preg_match(static::EVENT_COLLECTIONS_PATTERN, $collection_name)');
    return $this->collection_factory
      ->get($collection_name);
  }

  /**
   * @return \MongoCollection
   */
  public function templatesCollection() {
    return $this->message_templates;
  }

  /**
   * First index is on <line, timestamp> instead of <function, line, timestamp>,
   * because we write to this collection a lot, and the smaller index on two
   * numbers should be much faster to create than one with a string included.
   */
  public function ensureIndexes() {
    $templates = $this->message_templates;
    $indexes = array(
      // Index for adding/updating increments.
      array(
        'line' => 1,
        'timestamp' => -1,
      ),
      // Index for admin page without filters.
      array(
        'timestamp' => -1,
      ),
      // Index for admin page filtering by type.
      array(
        'type' => 1,
        'timestamp' => -1,
      ),
      // Index for admin page filtering by severity.
      array(
        'severity' => 1,
        'timestamp' => -1,
      ),
      // Index for admin page filtering by type and severity.
      array(
        'type' => 1,
        'severity' => 1,
        'timestamp' => -1,
      ),
    );
    foreach ($indexes as $index) {
      $templates
        ->ensureIndex($index);
    }
  }

  /**
   * Load a MongoDB watchdog event.
   *
   * @param string $id
   *
   * @return object|FALSE
   */
  function eventLoad($id) {
    $result = $this
      ->templatesCollection()
      ->findOne(array(
      '_id' => $id,
    ));
    return $result ? $result : FALSE;
  }

  /**
   * Drop the logger collections.
   *
   * @return int
   *   The number of collections dropped.
   */
  public function uninstall() {
    $count = 0;
    $collections = $this
      ->listCollections();
    foreach ($collections as $collection) {
      $name = $collection
        ->getName();
      if (preg_match(static::EVENT_COLLECTIONS_PATTERN, $name)) {
        $status = $collection
          ->drop();
        if ($status['ok'] == 1) {
          ++$count;
        }
      }
    }
    $status = $this->message_templates
      ->drop();
    if ($status['ok'] == 1) {
      ++$count;
    }
    return $count;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Logger::$collection_factory protected property
Logger::$message_templates protected property
Logger::ensureIndexes public function First index is on <line, timestamp> instead of <function, line, timestamp>, because we write to this collection a lot, and the smaller index on two numbers should be much faster to create than one with a string included.
Logger::eventCollection public function
Logger::eventLoad function Load a MongoDB watchdog event.
Logger::EVENT_COLLECTIONS_PATTERN constant
Logger::EVENT_COLLECTION_PREFIX constant
Logger::listCollections public function
Logger::log public function TODO implement this method to replace the code in mongodb_watchdog().
Logger::templatesCollection public function
Logger::TEMPLATE_COLLECTION constant
Logger::uninstall public function Drop the logger collections.
Logger::__construct public function