protected function normalize($data) { if (is_bool($data) || is_null($data)) { return var_export($data, true); } return parent::normalize($data); }
/** * {@inheritdoc} */ public function format(array $record) { $vars = parent::format($record); $output = $this->format; foreach ($vars['extra'] as $var => $val) { if (false !== strpos($output, '%extra.' . $var . '%')) { $output = str_replace('%extra.' . $var . '%', $this->stringify($val), $output); unset($vars['extra'][$var]); } } if ($this->ignoreEmptyContextAndExtra) { if (empty($vars['context'])) { unset($vars['context']); $output = str_replace('%context%', '', $output); } if (empty($vars['extra'])) { unset($vars['extra']); $output = str_replace('%extra%', '', $output); } } foreach ($vars as $var => $val) { if (false !== strpos($output, '%' . $var . '%')) { $output = str_replace('%' . $var . '%', $this->stringify($val), $output); } } return $output; }
/** * {@inheritdoc} */ public function format(array $record) { $record = parent::format($record); $record['tags'] = array(); $record['custom_data'] = array(); $record['timestamp'] = null; foreach (array('extra', 'context') as $source) { if (array_key_exists('tags', $record[$source]) && is_array($record[$source]['tags'])) { $record['tags'] = array_merge($record['tags'], $record[$source]['tags']); } if (array_key_exists('timestamp', $record[$source]) && is_numeric($record[$source]['timestamp'])) { $record['timestamp'] = $record[$source]['timestamp']; } unset($record[$source]['tags'], $record[$source]['timestamp']); } $record['custom_data'] = $record['extra']; $record['extra'] = array(); foreach ($record['context'] as $key => $item) { if (!in_array($key, array('file', 'line', 'exception'))) { $record['custom_data'][$key] = $item; unset($record['context'][$key]); } } return $record; }
/** * {@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; }
protected function normalize($data) { if (is_object($data) && !$data instanceof \DateTime) { return $data; } return parent::normalize($data); }
/** * {@inheritdoc} */ protected function normalize($data, int $depth = 0) { if (is_object($data) && !$data instanceof \DateTimeInterface) { return $data; } return parent::normalize($data, $depth); }
/** * {@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; }
/** * @param string $dateFormat The format of the timestamp: one supported by DateTime::format. * @param array $labeling Associative array of a Monolog record to a LTSV record mapping. * @param bool $includeContext Whether to include context fields in a LTSV record. * @param bool $includeExtra Whether to include extra fields in a LTSV record. * @param array $labelReplacement Rule of replacement for LTSV labels. * @param array $valueReplacement Rule of replacement for LTSV values. */ public function __construct($dateFormat = null, array $labeling = array('datetime' => 'time', 'level_name' => 'level', 'message' => 'message'), $includeContext = true, $includeExtra = true, array $labelReplacement = array("\r" => '', "\n" => '', "\t" => '', ':' => ''), array $valueReplacement = array("\r" => '\\r', "\n" => '\\n', "\t" => '\\t')) { parent::__construct($dateFormat); $this->labeling = $labeling; $this->includeContext = $includeContext; $this->includeExtra = $includeExtra; $this->labelReplacement = $labelReplacement; $this->valueReplacement = $valueReplacement; }
public function formatBatch(array $records) { $bulk = ['body' => []]; foreach ($records as $record) { $bulk['body'][] = ['index' => ['_index' => $this->index, '_type' => $this->type]]; $bulk['body'][] = parent::format($record); } return $bulk; }
protected function normalize($data) { $data = parent::normalize($data); if (is_array($data)) { foreach ($data as $key => &$value) { if (is_array($value)) { $value = json_encode($value); } } } return $data; }
/** * {@inheritdoc} */ public function format(array $record) { $vars = parent::format($record); $output = ''; $time = 0; if (!empty($vars['extra']['executionTime'])) { $time = $vars['extra']['executionTime']; } $output .= sprintf('[%10.3F]', round($time, 3)); $output .= ' ' . sprintf('(%-9s)', !empty($vars['level_name']) ? $vars['level_name'] : ''); $output .= ' ' . (!empty($vars['message']) ? $this->stringify($vars['message']) : ''); return $output . "\n"; }
/** * * {@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; }
/** * {@inheritdoc} */ public function format(array $record) { $vars = parent::format($record); $output = $this->format; foreach ($vars['extra'] as $var => $val) { if (false !== strpos($output, '%extra.' . $var . '%')) { $output = str_replace('%extra.' . $var . '%', $this->replaceNewlines($this->convertToString($val)), $output); unset($vars['extra'][$var]); } } foreach ($vars as $var => $val) { if (false !== strpos($output, '%' . $var . '%')) { $output = str_replace('%' . $var . '%', $this->replaceNewlines($this->convertToString($val)), $output); } } return $output; }
/** * {@inheritdoc} */ public function format(array $record) { $vars = parent::format($record); $output = "###############################################################\n###############################################################\n"; $output = $output . $this->format; $valores = ""; foreach ($vars['context'] as $var => $val) { $valores = $valores . " " . $var . ": " . $val . "\n"; } $output = str_replace('%context%', $valores, $output); foreach ($vars as $var => $val) { if (false !== strpos($output, '%' . $var . '%')) { $output = str_replace('%' . $var . '%', $this->convertToString($val), $output); } } return $output; }
/** * {@inheritdoc} */ public function format(array $record) { $record = parent::format($record); $message = $this->prepareMessage($record); return $this->toJson($message) . "\n"; }
/** * Write a log entry. * * Example: * ``` * use Google\Cloud\Logging\Logger; * * $psrLogger->log(Logger::ALERT, 'alert message'); * ``` * * ``` * // Write a log entry using the context array with placeholders. * use Google\Cloud\Logging\Logger; * * $psrLogger->log(Logger::ALERT, 'alert: {message}', [ * 'message' => 'my alert message' * ]); * ``` * * ``` * // Log information regarding an HTTP request * use Google\Cloud\Logging\Logger; * * $psrLogger->log(Logger::ALERT, 'alert message', [ * 'stackdriverOptions' => [ * 'httpRequest' => [ * 'requestMethod' => 'GET' * ] * ] * ]); * ``` * * @codingStandardsIgnoreStart * @param string|int $level The severity of the log entry. * @param string $message The message to log. * @param array $context { * Context is an associative array which can include placeholders to be * used in the `$message`. Placeholders must be delimited with a single * opening brace `{` and a single closing brace `}`. The context will be * added as additional information on the `jsonPayload`. Please note * that the key `stackdriverOptions` is reserved for logging Google * Stackdriver specific data. * * @type array $stackdriverOptions['resource'] The * [monitored resource](https://cloud.google.com/logging/docs/api/reference/rest/Shared.Types/MonitoredResource) * to associate this log entry with. **Defaults to** type global. * @type array $stackdriverOptions['httpRequest'] Information about the * HTTP request associated with this log entry, if applicable. * Please see * [the API docs](https://cloud.google.com/logging/docs/api/reference/rest/Shared.Types/LogEntry#httprequest) * for more information. * @type array $stackdriverOptions['labels'] A set of user-defined * (key, value) data that provides additional information about * the log entry. * @type array $stackdriverOptions['operation'] Additional information * about a potentially long-running operation with which a log * entry is associated. Please see * [the API docs](https://cloud.google.com/logging/docs/api/reference/rest/Shared.Types/LogEntry#logentryoperation) * for more information. * } * @throws InvalidArgumentException * @codingStandardsIgnoreEnd */ public function log($level, $message, array $context = []) { $this->validateLogLevel($level); $options = []; if (isset($context['exception']) && $context['exception'] instanceof \Exception) { $context['exception'] = (string) $context['exception']; } if (isset($context['stackdriverOptions'])) { $options = $context['stackdriverOptions']; unset($context['stackdriverOptions']); } $formatter = new NormalizerFormatter(); $processor = new PsrLogMessageProcessor(); $processedData = $processor(['message' => (string) $message, 'context' => $formatter->format($context)]); $jsonPayload = [$this->messageKey => $processedData['message']]; $entry = $this->logger->entry($jsonPayload + $processedData['context'], $options + ['severity' => $level]); $this->logger->write($entry); }
/** * @param string $dateFormat The format of the timestamp: one supported by DateTime::format */ public function __construct($dateFormat = null) { parent::__construct($dateFormat); }
/** * {@inheritdoc} */ public function format(array $record) { $record = parent::format($record); return $this->getDocument($record); }
public function format(array $record) { $normalized = parent::format($record); return LegacyLogger::format($normalized['channel'], $normalized['message'], $normalized); }
/** * {@inheritdoc} */ public function format(array $record) { $data = parent::format($record); return $this->toJson($data, true); }
public function testBatchFormat() { $formatter = new NormalizerFormatter('Y-m-d'); $formatted = $formatter->formatBatch(array(array('level_name' => 'CRITICAL', 'channel' => 'test', 'message' => 'bar', 'context' => array(), 'datetime' => new \DateTime(), 'extra' => array()), array('level_name' => 'WARNING', 'channel' => 'log', 'message' => 'foo', 'context' => array(), 'datetime' => new \DateTime(), 'extra' => array()))); $this->assertEquals(array(array('level_name' => 'CRITICAL', 'channel' => 'test', 'message' => 'bar', 'context' => array(), 'datetime' => date('Y-m-d'), 'extra' => array()), array('level_name' => 'WARNING', 'channel' => 'log', 'message' => 'foo', 'context' => array(), 'datetime' => date('Y-m-d'), 'extra' => array())), $formatted); }