mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-08 06:06:40 +02:00
Do not include message levels if it is already included in tag
This commit is contained in:
@@ -35,6 +35,9 @@ namespace Monolog\Formatter;
|
|||||||
|
|
||||||
class FluentdFormatter implements FormatterInterface
|
class FluentdFormatter implements FormatterInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var bool $levelTag - should message level be a part of the fluentd tag
|
||||||
|
*/
|
||||||
protected $levelTag = false;
|
protected $levelTag = false;
|
||||||
|
|
||||||
public function __construct($levelTag = false)
|
public function __construct($levelTag = false)
|
||||||
@@ -58,16 +61,22 @@ class FluentdFormatter implements FormatterInterface
|
|||||||
$tag .= '.' . strtolower($record['level_name']);
|
$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 '['
|
return '['
|
||||||
. '"' . $tag . '"'
|
. '"' . $tag . '"'
|
||||||
. ', '
|
. ', '
|
||||||
. $record['datetime']->getTimestamp()
|
. $record['datetime']->getTimestamp()
|
||||||
. ', '
|
. ', '
|
||||||
. json_encode(array(
|
. json_encode($message)
|
||||||
'message' => $record['message'],
|
|
||||||
'level' => $record['level'],
|
|
||||||
'level_name' => $record['level_name'],
|
|
||||||
'extra' => $record['extra']))
|
|
||||||
. ']';
|
. ']';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -35,18 +35,26 @@ class FluentdFormatterTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testFormat()
|
public function testFormat()
|
||||||
{
|
{
|
||||||
|
|
||||||
$record = $this->getRecord(Logger::WARNING);
|
$record = $this->getRecord(Logger::WARNING);
|
||||||
$record['datetime'] = new \DateTime("@0");
|
$record['datetime'] = new \DateTime("@0");
|
||||||
|
|
||||||
$formatter = new FluentdFormatter();
|
$formatter = new FluentdFormatter();
|
||||||
$this->assertEquals(
|
$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));
|
$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);
|
$formatter = new FluentdFormatter(true);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'["test.warning", 0, {"message":"test","level":300,"level_name":"WARNING","extra":[]}]',
|
'["test.error", 0, {"message":"test","extra":[]}]',
|
||||||
$formatter->format($record));
|
$formatter->format($record));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user