1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 13:16:39 +02:00

Do not include message levels if it is already included in tag

This commit is contained in:
fordnox
2015-11-15 21:23:25 +02:00
parent 6abfc22762
commit 034e895a6d
2 changed files with 25 additions and 8 deletions

View File

@@ -35,6 +35,9 @@ namespace Monolog\Formatter;
class FluentdFormatter implements FormatterInterface
{
/**
* @var bool $levelTag - should message level be a part of the fluentd tag
*/
protected $levelTag = false;
public function __construct($levelTag = false)
@@ -58,16 +61,22 @@ class FluentdFormatter implements FormatterInterface
$tag .= '.' . strtolower($record['level_name']);
}
$message = array(
'message' => $record['message'],
'extra' => $record['extra']
);
if (!$this->levelTag) {
$message['level'] = $record['level'];
$message['level_name'] = $record['level_name'];
}
return '['
. '"' . $tag . '"'
. ', '
. $record['datetime']->getTimestamp()
. ', '
. json_encode(array(
'message' => $record['message'],
'level' => $record['level'],
'level_name' => $record['level_name'],
'extra' => $record['extra']))
. json_encode($message)
. ']';
}

View File

@@ -35,18 +35,26 @@ class FluentdFormatterTest extends TestCase
*/
public function testFormat()
{
$record = $this->getRecord(Logger::WARNING);
$record['datetime'] = new \DateTime("@0");
$formatter = new FluentdFormatter();
$this->assertEquals(
'["test", 0, {"message":"test","level":300,"level_name":"WARNING","extra":[]}]',
'["test", 0, {"message":"test","extra":[],"level":300,"level_name":"WARNING"}]',
$formatter->format($record));
}
/**
* @covers Monolog\Formatter\FluentdFormatter::format
*/
public function testFormatWithTag()
{
$record = $this->getRecord(Logger::ERROR);
$record['datetime'] = new \DateTime("@0");
$formatter = new FluentdFormatter(true);
$this->assertEquals(
'["test.warning", 0, {"message":"test","level":300,"level_name":"WARNING","extra":[]}]',
'["test.error", 0, {"message":"test","extra":[]}]',
$formatter->format($record));
}
}