/** * {@inheritdoc} */ public function format(array $record) { $record = parent::format($record); if (!isset($record['datetime'], $record['message'], $record['level'])) { throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, ' . var_export($record, true) . ' given'); } $message = new Message(); $message->setTimestamp($record['datetime'])->setShortMessage((string) $record['message'])->setHost($this->systemName)->setLevel($this->logLevels[$record['level']]); if (isset($record['channel'])) { $message->setFacility($record['channel']); } if (isset($record['extra']['line'])) { $message->setLine($record['extra']['line']); unset($record['extra']['line']); } if (isset($record['extra']['file'])) { $message->setFile($record['extra']['file']); unset($record['extra']['file']); } foreach ($record['extra'] as $key => $val) { $message->setAdditional($this->extraPrefix . $key, is_scalar($val) ? $val : $this->toJson($val)); } foreach ($record['context'] as $key => $val) { $message->setAdditional($this->contextPrefix . $key, is_scalar($val) ? $val : $this->toJson($val)); } if (null === $message->getFile() && isset($record['context']['exception']['file'])) { if (preg_match("/^(.+):([0-9]+)\$/", $record['context']['exception']['file'], $matches)) { $message->setFile($matches[1]); $message->setLine($matches[2]); } } return $message; }
/** * {@inheritdoc} */ public function format(array $record) { if (isset($record['context'])) { $record['context'] = parent::format($record['context']); } if (isset($record['extra'])) { $record['extra'] = parent::format($record['extra']); } if (!isset($record['datetime'], $record['message'], $record['level'])) { throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, ' . var_export($record, true) . ' given'); } $message = new Message(); $message->setTimestamp($record['datetime'])->setShortMessage((string) $record['message'])->setHost($this->systemName)->setLevel($this->logLevels[$record['level']]); // start count with message length + system name length + 200 for padding / metadata $len = 200 + strlen((string) $record['message']) + strlen($this->systemName); if ($len > self::MAX_LENGTH) { $message->setShortMessage(substr($record['message'], 0, self::MAX_LENGTH - 200)); return $message; } if (isset($record['channel'])) { $message->setFacility($record['channel']); $len += strlen($record['channel']); } if (isset($record['extra']['line'])) { $message->setLine($record['extra']['line']); $len += 10; unset($record['extra']['line']); } if (isset($record['extra']['file'])) { $message->setFile($record['extra']['file']); $len += strlen($record['extra']['file']); unset($record['extra']['file']); } foreach ($record['extra'] as $key => $val) { $val = is_scalar($val) || null === $val ? $val : $this->toJson($val); $len += strlen($this->extraPrefix . $key . $val); if ($len > self::MAX_LENGTH) { $message->setAdditional($this->extraPrefix . $key, substr($val, 0, self::MAX_LENGTH - $len)); break; } $message->setAdditional($this->extraPrefix . $key, $val); } foreach ($record['context'] as $key => $val) { $val = is_scalar($val) || null === $val ? $val : $this->toJson($val); $len += strlen($this->contextPrefix . $key . $val); if ($len > self::MAX_LENGTH) { $message->setAdditional($this->contextPrefix . $key, substr($val, 0, self::MAX_LENGTH - $len)); break; } $message->setAdditional($this->contextPrefix . $key, $val); } if (null === $message->getFile() && isset($record['context']['exception']['file'])) { if (preg_match("/^(.+):([0-9]+)\$/", $record['context']['exception']['file'], $matches)) { $message->setFile($matches[1]); $message->setLine($matches[2]); } } return $message; }
/** * {@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); }
protected function setValuesToMessage(Message $message) { $message->setFacility('facility'); $message->setFile('foo.php'); $message->setFullMessage('Full Message'); $message->setHost('example.com'); $message->setLevel(3); $message->setLine(42); $message->setShortMessage('Short Message'); $message->setTimestamp(123456789); $message->setVersion('1.2.3'); }
/** * * {@inheritdoc} * */ public function format(array $record) { $record = parent::format($record); $message = new Message(); $message->setTimestamp($record['datetime'])->setShortMessage((string) $record['message'])->setFacility($record['channel'])->setHost($this->systemName)->setLine(isset($record['extra']['line']) ? $record['extra']['line'] : null)->setFile(isset($record['extra']['file']) ? $record['extra']['file'] : null)->setLevel($this->logLevels[$record['level']]); // Do not duplicate these values in the additional fields unset($record['extra']['line']); unset($record['extra']['file']); foreach ($record['extra'] as $key => $val) { $message->setAdditional($this->extraPrefix . $key, is_scalar($val) ? $val : $this->toJson($val)); } foreach ($record['context'] as $key => $val) { $message->setAdditional($this->contextPrefix . $key, is_scalar($val) ? $val : $this->toJson($val)); } return $message; }
/** * @access protected * @param LogRecord * @return void */ protected function processRecord(\Naucon\Logger\LogRecord $logRecord) { $message = new Message(); $message->setLevel($logRecord->getLevel()); $message->setShortMessage($logRecord->getMessage()); $message->setHost($this->getHost()); $message->setFacility($this->getFacility()); if (method_exists($logRecord, 'getCreated()')) { $message->setTimestamp($logRecord->getCreated()); } else { $message->setTimestamp(time(true)); } $context = $logRecord->getContext(); if (isset($context['exception']) && $context['exception'] instanceof \Exception) { /** * @var \Exception $exception */ $exception = $context['exception']; $message->setFullMessage($exception->getTraceAsString()); $message->setFile($exception->getFile()); $message->setLine($exception->getLine()); } // $message->setAdditional("Additional Field 1", "bla bla"); // $message->setAdditional("Additional Field 2", "lirum larum"); $this->getPublisher()->publish($message); }
public function testMethodChaining() { $this->message->setTimestamp(new \DateTime())->setAdditional("test", "value")->setFacility("test")->setHost("test")->setFile("test")->setFullMessage("testtest")->setShortMessage("test")->setLevel("ERROR")->setLine(1); }
/** * @param array $event * * @return Message */ public function format($event) { $message = new Message(); $message->setHost(gethostname()); if (isset($event['priority'])) { $message->setLevel($event['priority']); } else { if (isset($event['errno'])) { // @todo Convert to syslog error levels? $message->setLevel($event['errno']); } } $message->setFullMessage($event['message']); $message->setShortMessage($event['message']); if (isset($event['full'])) { $message->setFullMessage($event['full']); } if (isset($event['short'])) { $message->setShortMessage($event['short']); } if (isset($event['file'])) { $message->setFile($event['file']); } if (isset($event['line'])) { $message->setLine($event['line']); } if (isset($event['version'])) { $message->setVersion($event['version']); } if (isset($event['facility'])) { $message->setFacility($event['facility']); } else { $message->setFacility($this->_sFacility); } if (isset($event['extra']) && isset($event['extra']['trace'])) { $aTraceOut = []; foreach ($event['extra']['trace'] as $aTrace) { if (isset($aTrace['file']) && isset($aTrace['line'])) { $aTraceOut[] = sprintf('file: %s | line: %s', $aTrace['file'], $aTrace['line']); } elseif (isset($aTrace['function']) && isset($aTrace['class']) && isset($aTrace['type'])) { $aTraceOut[] = sprintf('%s %s %s', $aTrace['class'], $aTrace['type'], $aTrace['function']); } } $message->setAdditional('trace', implode(PHP_EOL, $aTraceOut)); } if ($this->_sLogname != '') { $message->setAdditional('logname', $this->_sLogname); } if (isset($event['timestamp'])) { if ($event['timestamp'] instanceof \DateTime) { $message->setTimestamp($event['timestamp']->getTimestamp()); } else { $message->setTimestamp($event['timestamp']); } } $aBlackKeys = ['message', 'priority', 'errno', 'full', 'short', 'file', 'line', 'version', 'facility', 'timestamp']; foreach ($event as $mKey => $mValue) { if (!in_array($mKey, $aBlackKeys)) { if (is_array($mValue)) { $rows = []; foreach ($mValue as $mKey2 => $mValue2) { if (is_array($mValue2)) { continue; } $rows[] = sprintf('%s: %s', $mKey2, $mValue2); } $mValue = implode(' | ', $rows); } $message->setAdditional($mKey, $mValue); } } return $message; }