You are here

class RulesLog in Rules 7.2

The rules default logging class.

Hierarchy

Expanded class hierarchy of RulesLog

1 string reference to 'RulesLog'
rules_show_debug_output in ./rules.module
Returns whether the debug log should be shown.

File

includes/rules.core.inc, line 2664
Rules base classes and interfaces needed for any rule evaluation.

View source
class RulesLog {
  const INFO = 1;
  const WARN = 2;
  const ERROR = 3;
  protected static $logger;

  /**
   * @return RulesLog
   *   Returns the rules logger instance.
   */
  public static function logger() {
    if (!isset(self::$logger)) {
      $class = __CLASS__;
      self::$logger = new $class(variable_get('rules_log_level', self::INFO));
    }
    return self::$logger;
  }
  protected $log = array();
  protected $logLevel;
  protected $line = 0;

  /**
   * This is a singleton.
   */
  protected function __construct($logLevel = self::WARN) {
    $this->logLevel = $logLevel;
  }
  public function __clone() {
    throw new Exception("Cannot clone the logger.");
  }

  /**
   * Logs a log message.
   *
   * @see rules_log()
   */
  public function log($msg, $args = array(), $logLevel = self::INFO, $scope = NULL, $path = NULL) {
    if ($logLevel >= $this->logLevel) {
      $this->log[] = array(
        $msg,
        $args,
        $logLevel,
        microtime(TRUE),
        $scope,
        $path,
      );
    }
  }

  /**
   * Gets an array of logged messages.
   */
  public function get() {
    return $this->log;
  }

  /**
   * Clears the logged messages.
   */
  public function clear() {
    $this->log = array();
  }

  /**
   * Checks the log and throws an exception if there were any problems.
   */
  public function checkLog($logLevel = self::WARN) {
    foreach ($this->log as $entry) {
      if ($entry[2] >= $logLevel) {
        throw new Exception($this
          ->render());
      }
    }
  }

  /**
   * Checks the log for error messages.
   *
   * @param int $logLevel
   *   Lowest log level to return. Values lower than $logLevel will not be
   *   returned.
   *
   * @return bool
   *   Whether the an error has been logged.
   */
  public function hasErrors($logLevel = self::WARN) {
    foreach ($this->log as $entry) {
      if ($entry[2] >= $logLevel) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Renders the whole log.
   */
  public function render() {
    $line = 0;
    $output = array();
    while (isset($this->log[$line])) {
      $vars['head'] = t($this->log[$line][0], $this->log[$line][1]);
      $vars['log'] = $this
        ->renderHelper($line);
      $output[] = theme('rules_debug_element', $vars);
      $line++;
    }
    return implode('', $output);
  }

  /**
   * Renders the log of one event invocation.
   */
  protected function renderHelper(&$line = 0) {
    $startTime = isset($this->log[$line][3]) ? $this->log[$line][3] : 0;
    $output = array();
    while ($line < count($this->log)) {
      if ($output && !empty($this->log[$line][4])) {

        // The next entry stems from another evaluated set, add in its log
        // messages here.
        $vars['head'] = t($this->log[$line][0], $this->log[$line][1]);
        if (isset($this->log[$line][5])) {
          $vars['link'] = '[' . l(t('edit'), $this->log[$line][5]) . ']';
        }
        $vars['log'] = $this
          ->renderHelper($line);
        $output[] = theme('rules_debug_element', $vars);
      }
      else {
        $formatted_diff = round(($this->log[$line][3] - $startTime) * 1000, 3) . ' ms';
        $msg = $formatted_diff . ' ' . t($this->log[$line][0], $this->log[$line][1]);
        if ($this->log[$line][2] >= RulesLog::WARN) {
          $level = $this->log[$line][2] == RulesLog::WARN ? 'warn' : 'error';
          $msg = '<span class="rules-debug-' . $level . '">' . $msg . '</span>';
        }
        if (isset($this->log[$line][5]) && !isset($this->log[$line][4])) {
          $msg .= ' [' . l(t('edit'), $this->log[$line][5]) . ']';
        }
        $output[] = $msg;
        if (isset($this->log[$line][4]) && !$this->log[$line][4]) {

          // This was the last log entry of this set.
          return theme('item_list', array(
            'items' => $output,
          ));
        }
      }
      $line++;
    }
    return theme('item_list', array(
      'items' => $output,
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesLog::$line protected property
RulesLog::$log protected property
RulesLog::$logger protected static property
RulesLog::$logLevel protected property
RulesLog::checkLog public function Checks the log and throws an exception if there were any problems.
RulesLog::clear public function Clears the logged messages.
RulesLog::ERROR constant
RulesLog::get public function Gets an array of logged messages.
RulesLog::hasErrors public function Checks the log for error messages.
RulesLog::INFO constant
RulesLog::log public function Logs a log message.
RulesLog::logger public static function
RulesLog::render public function Renders the whole log.
RulesLog::renderHelper protected function Renders the log of one event invocation.
RulesLog::WARN constant
RulesLog::__clone public function
RulesLog::__construct protected function This is a singleton.