public function format(array $record) { if (!isset($record['context']['exception']) || !$record['context']['exception'] instanceof \Exception) { return $this->formatter->format($record); } /** @var \Exception $exception */ $exception = $record['context']['exception']; $fullMessage = sprintf("%s: \"%s\"\n\n%s", get_class($exception), $exception->getMessage(), $exception->getTraceAsString()); unset($record['context']['exception']); /** @var Message $message */ $message = $this->formatter->format($record); $message->setFullMessage($fullMessage); return $message; }
/** * @covers Monolog\Formatter\GelfMessageFormatter::format */ public function testFormatWithExtra() { $formatter = new GelfMessageFormatter(); $record = array('level' => Logger::ERROR, 'level_name' => 'ERROR', 'channel' => 'meh', 'context' => array('from' => 'logger'), 'datetime' => new \DateTime("@0"), 'extra' => array('key' => 'pair'), 'message' => 'log'); $message = $formatter->format($record); $this->assertInstanceOf('Gelf\\Message', $message); $message_array = $message->toArray(); $this->assertArrayHasKey('_key', $message_array); $this->assertEquals('pair', $message_array['_key']); // Test with extraPrefix $formatter = new GelfMessageFormatter(null, 'EXT'); $message = $formatter->format($record); $this->assertInstanceOf('Gelf\\Message', $message); $message_array = $message->toArray(); $this->assertArrayHasKey('_EXTkey', $message_array); $this->assertEquals('pair', $message_array['_EXTkey']); }
public function testFormatWithLargeData() { $formatter = new GelfMessageFormatter(); $record = array('level' => Logger::ERROR, 'level_name' => 'ERROR', 'channel' => 'meh', 'context' => array('exception' => str_repeat(' ', 32767)), 'datetime' => new \DateTime("@0"), 'extra' => array('key' => str_repeat(' ', 32767)), 'message' => 'log'); $message = $formatter->format($record); $messageArray = $message->toArray(); // 200 for padding + metadata $length = 200; foreach ($messageArray as $key => $value) { if (!in_array($key, array('level', 'timestamp'))) { $length += strlen($value); } } // in graylog2/gelf-php before 1.4.1 empty strings are filtered and won't be included in the message // though it should be sufficient to ensure that the entire message length does not exceed the maximum // length being allowed $this->assertLessThanOrEqual(32766, $length, 'The message length is no longer than the maximum allowed length'); }
public function testFormatWithLargeData() { $formatter = new GelfMessageFormatter(); $record = array('level' => Logger::ERROR, 'level_name' => 'ERROR', 'channel' => 'meh', 'context' => array('exception' => str_repeat(' ', 32767)), 'datetime' => new \DateTime("@0"), 'extra' => array('key' => str_repeat(' ', 32767)), 'message' => 'log'); $message = $formatter->format($record); $messageArray = $message->toArray(); $this->assertLessThanOrEqual(32766, strlen($messageArray['_key'])); $this->assertLessThanOrEqual(32766, strlen($messageArray['_ctxt_exception'])); }