1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-31 02:10:22 +02:00

Make gelf formatter extend the normalizer one

This commit is contained in:
Jordi Boggiano
2012-04-22 17:38:31 +02:00
parent 985e682079
commit 56d246cca3
3 changed files with 18 additions and 26 deletions

View File

@@ -19,7 +19,7 @@ use Gelf\Message;
*
* @author Matt Lehner <mlehner@gmail.com>
*/
class GelfMessageFormatter implements FormatterInterface
class GelfMessageFormatter extends NormalizerFormatter
{
/**
* @var string the name of the system for the Gelf log message
@@ -35,7 +35,7 @@ class GelfMessageFormatter implements FormatterInterface
* @var string a prefix for 'context' fields from the Monolog record (optional)
*/
protected $contextPrefix;
/**
* Translates Monolog log levels to Graylog2 log priorities.
*/
@@ -50,6 +50,8 @@ class GelfMessageFormatter implements FormatterInterface
public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
{
parent::__construct('U.u');
$this->systemName = $systemName ?: gethostname();
$this->extraPrefix = $extraPrefix;
@@ -61,42 +63,29 @@ class GelfMessageFormatter implements FormatterInterface
*/
public function format(array $record)
{
$record = parent::format($record);
$message = new Message();
$message
->setTimestamp($record['datetime']->format('U.u'))
->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'] ]);
->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 : json_encode($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 : json_encode($val));
$message->setAdditional($this->contextPrefix . $key, is_scalar($val) ? $val : $this->toJson($val));
}
return $message;
}
/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
{
$messages = array();
foreach ($records as $record) {
$messages[] = $this->format($record);
}
return $messages;
}
}

View File

@@ -76,12 +76,15 @@ class NormalizerFormatter implements FormatterInterface
return '[resource]';
}
return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data));
}
protected function toJson($data)
{
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$encoded = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} else {
$encoded = json_encode($data);
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
return sprintf("[object] (%s: %s)", get_class($data), $encoded);
return json_encode($data);
}
}

View File

@@ -63,7 +63,7 @@ class GelfHandlerTest extends TestCase
$record = $this->getRecord(Logger::DEBUG, "A test debug message");
$handler->handle($record);
$this->assertEquals(7, $messagePublisher->lastMessage->getLevel());
$this->assertEquals(LOG_DEBUG, $messagePublisher->lastMessage->getLevel());
$this->assertEquals('test', $messagePublisher->lastMessage->getFacility());
$this->assertEquals($record['message'], $messagePublisher->lastMessage->getShortMessage());
$this->assertEquals(null, $messagePublisher->lastMessage->getFullMessage());
@@ -77,7 +77,7 @@ class GelfHandlerTest extends TestCase
$record = $this->getRecord(Logger::WARNING, "A test warning message");
$handler->handle($record);
$this->assertEquals(4, $messagePublisher->lastMessage->getLevel());
$this->assertEquals(LOG_WARNING, $messagePublisher->lastMessage->getLevel());
$this->assertEquals('test', $messagePublisher->lastMessage->getFacility());
$this->assertEquals($record['message'], $messagePublisher->lastMessage->getShortMessage());
$this->assertEquals(null, $messagePublisher->lastMessage->getFullMessage());