mirror of
https://github.com/Seldaek/monolog.git
synced 2025-07-30 18:00:17 +02:00
PsrLogMessageProcessor: add option to remove used context fields
This commit is contained in:
@@ -24,12 +24,17 @@ class PsrLogMessageProcessor
|
||||
|
||||
private $dateFormat;
|
||||
|
||||
/** @var bool */
|
||||
private $removeUsedContextFields;
|
||||
|
||||
/**
|
||||
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
|
||||
* @param bool $removeUsedContextFields If set to true the fields interpolated into message gets unset
|
||||
*/
|
||||
public function __construct(string $dateFormat = null)
|
||||
public function __construct(string $dateFormat = null, bool $removeUsedContextFields = false)
|
||||
{
|
||||
$this->dateFormat = null === $dateFormat ? static::SIMPLE_DATE : $dateFormat;
|
||||
$this->removeUsedContextFields = $removeUsedContextFields;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,14 +49,23 @@ class PsrLogMessageProcessor
|
||||
|
||||
$replacements = [];
|
||||
foreach ($record['context'] as $key => $val) {
|
||||
$placeholder = '{' . $key . '}';
|
||||
if (strpos($record['message'], $placeholder) === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) {
|
||||
$replacements['{'.$key.'}'] = $val;
|
||||
$replacements[$placeholder] = $val;
|
||||
} elseif ($val instanceof \DateTimeInterface) {
|
||||
$replacements['{'.$key.'}'] = $val->format($this->dateFormat);
|
||||
$replacements[$placeholder] = $val->format($this->dateFormat);
|
||||
} elseif (is_object($val)) {
|
||||
$replacements['{'.$key.'}'] = '[object '.get_class($val).']';
|
||||
$replacements[$placeholder] = '[object '.get_class($val).']';
|
||||
} else {
|
||||
$replacements['{'.$key.'}'] = '['.gettype($val).']';
|
||||
$replacements[$placeholder] = '['.gettype($val).']';
|
||||
}
|
||||
|
||||
if ($this->removeUsedContextFields) {
|
||||
unset($record['context'][$key]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -25,6 +25,19 @@ class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
|
||||
'context' => ['foo' => $val],
|
||||
]);
|
||||
$this->assertEquals($expected, $message['message']);
|
||||
$this->assertSame(['foo' => $val], $message['context']);
|
||||
}
|
||||
|
||||
public function testReplacementWithContextRemoval()
|
||||
{
|
||||
$proc = new PsrLogMessageProcessor($dateFormat = null, $removeUsedContextFields = true);
|
||||
|
||||
$message = $proc([
|
||||
'message' => '{foo}',
|
||||
'context' => ['foo' => 'bar', 'lorem' => 'ipsum'],
|
||||
]);
|
||||
$this->assertSame('bar', $message['message']);
|
||||
$this->assertSame(['lorem' => 'ipsum'], $message['context']);
|
||||
}
|
||||
|
||||
public function testCustomDateFormat()
|
||||
@@ -39,6 +52,7 @@ class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
|
||||
'context' => ['foo' => $date],
|
||||
]);
|
||||
$this->assertEquals($date->format($format), $message['message']);
|
||||
$this->assertSame(['foo' => $date], $message['context']);
|
||||
}
|
||||
|
||||
public function getPairs()
|
||||
|
Reference in New Issue
Block a user