示例#1
0
 /**
  * {@inheritdoc}
  */
 public function report($priority, $message, $extra = [])
 {
     $grayLogMessage = new Message();
     $grayLogMessage->setShortMessage($message);
     $grayLogMessage->setFullMessage($extra ? print_r($extra, true) : $message);
     $grayLogMessage->setHost((Console::isConsole() ? 'Console' : $_SERVER['HTTP_HOST']));
     $grayLogMessage->setLevel($priority);
     $grayLogMessage->setTimestamp(time());
     $grayLogMessage->setVersion('1.0');
     $this->publisher->publish($grayLogMessage);
 }
示例#2
0
 /**
  * Processes log messages and sends them to specific destination.
  * Derived child classes must implement this method.
  * @param array $logs list of messages. Each array element represents one message
  * with the following structure:
  * array(
  *   [0] => message (string)
  *   [1] => level (string)
  *   [2] => category (string)
  *   [3] => timestamp (float, obtained by microtime(true));
  */
 protected function processLogs($logs)
 {
     $publisher = new Gelf\Publisher($this->transport);
     foreach ($logs as $logItem) {
         list($message, $level, $category, $timestamp) = $logItem;
         $gelfMessage = new Gelf\Message();
         if (is_string($message)) {
             $gelfMessage->setShortMessage($message);
         } elseif (!is_array($message)) {
             $gelfMessage->setShortMessage(var_export($message, true));
         } else {
             if (isset($message['message'])) {
                 $gelfMessage->setShortMessage($message['message']);
                 unset($message['message']);
                 foreach ($message as $key => $val) {
                     if (is_string($key)) {
                         $gelfMessage->setAdditional($key, is_string($val) ? $val : var_export($val, true));
                     }
                 }
             } else {
                 $gelfMessage->setShortMessage(var_export($message, true));
             }
         }
         $gelfMessage->setLevel(($_ =& $this->_levels[$level]) ?: LogLevel::INFO)->setTimestamp($timestamp)->setFacility($category)->setAdditional('level_name', strtolower($level));
         foreach ($this->extra as $key => $val) {
             if (is_string($key)) {
                 $gelfMessage->setAdditional($key, is_string($val) ? $val : var_export($val, true));
             }
         }
         if (isset($logItem[4]) && is_array($logItem[4])) {
             $traces = [];
             foreach ($logItem[4] as $index => $trace) {
                 $traces[] = "{$trace['file']}:{$trace['line']}";
                 if (0 === $index) {
                     $gelfMessage->setFile($trace['file']);
                     $gelfMessage->setLine($trace['line']);
                 }
             }
             $gelfMessage->setAdditional('trace', implode("\n", $traces));
         }
         // Publishing message
         $publisher->publish($gelfMessage);
     }
 }
示例#3
0
 /**
  * @expectedException RuntimeException
  */
 public function testMissingTransport()
 {
     $publisher = new Publisher(null, $this->messageValidator);
     $this->assertEquals(0, count($publisher->getTransports()));
     $publisher->publish($this->message);
 }