mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-03 11:47:38 +02:00
Add context.* replacement in line formatter, fixes #717
This commit is contained in:
@@ -76,6 +76,13 @@ class LineFormatter extends NormalizerFormatter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($vars['context'] as $var => $val) {
|
||||||
|
if (false !== strpos($output, '%context.'.$var.'%')) {
|
||||||
|
$output = str_replace('%context.'.$var.'%', $this->stringify($val), $output);
|
||||||
|
unset($vars['context'][$var]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->ignoreEmptyContextAndExtra) {
|
if ($this->ignoreEmptyContextAndExtra) {
|
||||||
if (empty($vars['context'])) {
|
if (empty($vars['context'])) {
|
||||||
unset($vars['context']);
|
unset($vars['context']);
|
||||||
|
@@ -91,6 +91,20 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
|
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testContextAndExtraReplacement()
|
||||||
|
{
|
||||||
|
$formatter = new LineFormatter('%context.foo% => %extra.foo%');
|
||||||
|
$message = $formatter->format(array(
|
||||||
|
'level_name' => 'ERROR',
|
||||||
|
'channel' => 'meh',
|
||||||
|
'context' => array('foo' => 'bar'),
|
||||||
|
'datetime' => new \DateTime,
|
||||||
|
'extra' => array('foo' => 'xbar'),
|
||||||
|
'message' => 'log',
|
||||||
|
));
|
||||||
|
$this->assertEquals('bar => xbar', $message);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDefFormatWithObject()
|
public function testDefFormatWithObject()
|
||||||
{
|
{
|
||||||
$formatter = new LineFormatter(null, 'Y-m-d');
|
$formatter = new LineFormatter(null, 'Y-m-d');
|
||||||
|
Reference in New Issue
Block a user