Esempio n. 1
0
 /**
  * Add a message as a log entry
  *
  * @param  int $priority
  * @param  mixed $message
  * @param  array|Traversable $extra
  * @return Logger
  * @throws Exception\InvalidArgumentException if message can't be cast to string
  * @throws Exception\InvalidArgumentException if extra can't be iterated over
  * @throws Exception\RuntimeException if no log writer specified
  */
 public function log($priority, $message, $extra = array())
 {
     if (!is_int($priority) || $priority < 0 || $priority >= count($this->priorities)) {
         throw new Exception\InvalidArgumentException(sprintf('$priority must be an integer > 0 and < %d; received %s', count($this->priorities), var_export($priority, 1)));
     }
     if (is_object($message) && !method_exists($message, '__toString')) {
         throw new Exception\InvalidArgumentException('$message must implement magic __toString() method');
     }
     if (!is_array($extra) && !$extra instanceof Traversable) {
         throw new Exception\InvalidArgumentException('$extra must be an array or implement Traversable');
     } elseif ($extra instanceof Traversable) {
         $extra = ArrayUtils::iteratorToArray($extra);
     }
     if ($this->writers->count() === 0) {
         throw new Exception\RuntimeException('No log writer specified');
     }
     $timestamp = new DateTime();
     if (is_array($message)) {
         $message = var_export($message, true);
     }
     $event = array('timestamp' => $timestamp, 'priority' => (int) $priority, 'priorityName' => $this->priorities[$priority], 'message' => (string) $message, 'extra' => $extra);
     foreach ($this->processors->toArray() as $processor) {
         $event = $processor->process($event);
     }
     foreach ($this->writers->toArray() as $writer) {
         $writer->write($event);
     }
     return $this;
 }
Esempio n. 2
0
 /**
  * Add a message as a log entry
  *
  * @param  int                   $priority
  * @param  mixed                 $message
  * @param  array|Traversable|int $extra
  *
  * @throws \RuntimeException
  * @throws \InvalidArgumentException if extra can't be iterated over
  * @return self
  */
 public function log($priority, $message, $extra = array())
 {
     if (!is_int($priority) || !isset($this->priorities[$priority])) {
         throw new \InvalidArgumentException('Invalid priority');
     }
     if (is_object($message) && !method_exists($message, '__toString')) {
         throw new \InvalidArgumentException('$message must implement magic __toString() method');
     }
     if (is_int($extra)) {
         $time = $extra;
         $extra = array();
     } elseif (isset($extra['time'])) {
         $time = $extra['time'];
         unset($extra['time']);
     } else {
         $time = microtime(true);
     }
     if (!is_array($extra) && !$extra instanceof Traversable) {
         throw new \InvalidArgumentException('$extra must be an array or implement Traversable');
     } elseif ($extra instanceof Traversable) {
         $extra = ArrayUtils::iteratorToArray($extra);
     }
     if ($this->writers->count() === 0) {
         throw new \RuntimeException('No log writer specified');
     }
     if ($this->dateTimeFormat) {
         $time = time($time, $this->dateTimeFormat);
     }
     if (is_array($message)) {
         $message = var_export($message, true);
     }
     foreach ($this->writers->toArray() as $writer) {
         $writer->write(array('timestamp' => $time, 'priority' => (int) $priority, 'priorityName' => $this->priorities[$priority], 'message' => (string) $message, 'extra' => $extra));
     }
     return $this;
 }