From bbf9de5c8d29763d64230645b653cc63ef393882 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 18 Jun 2018 16:48:08 +0200 Subject: [PATCH] Follow useMicroseconds flag when possible --- src/Monolog/Processor/PsrLogMessageProcessor.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Processor/PsrLogMessageProcessor.php b/src/Monolog/Processor/PsrLogMessageProcessor.php index a9d2e70d..f845f883 100644 --- a/src/Monolog/Processor/PsrLogMessageProcessor.php +++ b/src/Monolog/Processor/PsrLogMessageProcessor.php @@ -33,7 +33,7 @@ class PsrLogMessageProcessor */ public function __construct(string $dateFormat = null, bool $removeUsedContextFields = false) { - $this->dateFormat = null === $dateFormat ? static::SIMPLE_DATE : $dateFormat; + $this->dateFormat = $dateFormat; $this->removeUsedContextFields = $removeUsedContextFields; } @@ -57,7 +57,13 @@ class PsrLogMessageProcessor if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) { $replacements[$placeholder] = $val; } elseif ($val instanceof \DateTimeInterface) { - $replacements[$placeholder] = $val->format($this->dateFormat); + if (!$this->dateFormat && $val instanceof \Monolog\DateTimeImmutable) { + // handle monolog dates using __toString if no specific dateFormat was asked for + // so that it follows the useMicroseconds flag + $replacements[$placeholder] = (string) $val; + } else { + $replacements[$placeholder] = $val->format($this->dateFormat ?: static::SIMPLE_DATE); + } } elseif (is_object($val)) { $replacements[$placeholder] = '[object '.get_class($val).']'; } elseif (is_array($val)) {