1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-21 16:46:11 +02:00

Handle DateTime objects in formatted messages (#940)

* Handle DateTime objects in formatted meessages

* Use interface to catch both DateTime and DateTimeImmutable

* Maintain formatting standards.

* Visibility to private.
This commit is contained in:
Piers Warmers
2017-03-14 18:07:57 +11:00
committed by Jordi Boggiano
parent f3dda67c09
commit a0406bf8dd
2 changed files with 31 additions and 0 deletions

View File

@@ -20,6 +20,18 @@ namespace Monolog\Processor;
*/
class PsrLogMessageProcessor
{
const SIMPLE_DATE = "Y-m-d\TH:i:sP";
private $dateFormat;
/**
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
*/
public function __construct(string $dateFormat = null)
{
$this->dateFormat = null === $dateFormat ? static::SIMPLE_DATE : $dateFormat;
}
/**
* @param array $record
* @return array
@@ -34,6 +46,8 @@ class PsrLogMessageProcessor
foreach ($record['context'] as $key => $val) {
if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) {
$replacements['{'.$key.'}'] = $val;
} elseif ($val instanceof \DateTimeInterface) {
$replacements['{'.$key.'}'] = $val->format($this->dateFormat);
} elseif (is_object($val)) {
$replacements['{'.$key.'}'] = '[object '.get_class($val).']';
} else {