[GH-ISSUE #65] Logger->addRecord() broken for locales using , as decimal digit separator #16

Closed
opened 2026-03-04 02:11:24 +03:00 by kerem · 5 comments
Owner

Originally created by @hanneskod on GitHub (Mar 13, 2012).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/65

Some locales use , as decimal digit separator instead of . This breaks datetime creation in Logger->addRecord().

I ran across this bugg using:
setlocale(LC_ALL, "sv_SE.utf8", "sv_SE", "sv");

Originally created by @hanneskod on GitHub (Mar 13, 2012). Original GitHub issue: https://github.com/Seldaek/monolog/issues/65 Some locales use , as decimal digit separator instead of . This breaks datetime creation in Logger->addRecord(). I ran across this bugg using: setlocale(LC_ALL, "sv_SE.utf8", "sv_SE", "sv");
kerem closed this issue 2026-03-04 02:11:24 +03:00
Author
Owner

@hanneskod commented on GitHub (Mar 13, 2012):

Here is a quick fix:

*** Monolog/Logger.php  2012-03-13 13:27:21.343220815 +0100
--- local/Logger.php    2012-03-13 12:29:59.531228485 +0100
***************
*** 162,174 ****
          if (!$this->handlers) {
              $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
          }
          $record = array(
              'message' => (string) $message,
              'context' => $context,
              'level' => $level,
              'level_name' => self::getLevelName($level),
              'channel' => $this->name,
!             'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6f', microtime(true))),
              'extra' => array(),
          );
          // check if any message will handle this message
--- 162,179 ----
          if (!$this->handlers) {
              $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
          }
+         
+         // Replace , with . in time to support locales using , as decimal digit separator
+         $time = sprintf('%.6f', microtime(true));
+         $time = str_replace(',', '.', $time);
+         
          $record = array(
              'message' => (string) $message,
              'context' => $context,
              'level' => $level,
              'level_name' => self::getLevelName($level),
              'channel' => $this->name,
!             'datetime' => \DateTime::createFromFormat('U.u', $time),
              'extra' => array(),
          );
          // check if any message will handle this message
<!-- gh-comment-id:4472359 --> @hanneskod commented on GitHub (Mar 13, 2012): Here is a quick fix: ``` *** Monolog/Logger.php 2012-03-13 13:27:21.343220815 +0100 --- local/Logger.php 2012-03-13 12:29:59.531228485 +0100 *************** *** 162,174 **** if (!$this->handlers) { $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG)); } $record = array( 'message' => (string) $message, 'context' => $context, 'level' => $level, 'level_name' => self::getLevelName($level), 'channel' => $this->name, ! 'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6f', microtime(true))), 'extra' => array(), ); // check if any message will handle this message --- 162,179 ---- if (!$this->handlers) { $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG)); } + + // Replace , with . in time to support locales using , as decimal digit separator + $time = sprintf('%.6f', microtime(true)); + $time = str_replace(',', '.', $time); + $record = array( 'message' => (string) $message, 'context' => $context, 'level' => $level, 'level_name' => self::getLevelName($level), 'channel' => $this->name, ! 'datetime' => \DateTime::createFromFormat('U.u', $time), 'extra' => array(), ); // check if any message will handle this message ```
Author
Owner

@Seldaek commented on GitHub (Mar 14, 2012):

Thanks for reporting and for the proposed fix, I'll take a look later this week hopefully, crazy busy right now no time to look in detail.

<!-- gh-comment-id:4493873 --> @Seldaek commented on GitHub (Mar 14, 2012): Thanks for reporting and for the proposed fix, I'll take a look later this week hopefully, crazy busy right now no time to look in detail.
Author
Owner

@Seldaek commented on GitHub (Apr 22, 2012):

I wasn't able to reproduce unfortunately, but I think the fix I pushed is correct nonetheless. Please check and confirm.

<!-- gh-comment-id:5269003 --> @Seldaek commented on GitHub (Apr 22, 2012): I wasn't able to reproduce unfortunately, but I think the fix I pushed is correct nonetheless. Please check and confirm.
Author
Owner

@hanneskod commented on GitHub (Apr 23, 2012):

Fix is confirmed. I guess the locale in question have to be installed in your OS to reproduce this behavoir...

A really nice fix I must say though. For someone like myself, not comming from the world of C, the sprintf function often seams like magic. This time all that was needed was a 10 sec. look at the documentation. So it goes!

<!-- gh-comment-id:5277825 --> @hanneskod commented on GitHub (Apr 23, 2012): Fix is confirmed. I guess the locale in question have to be installed in your OS to reproduce this behavoir... A really nice fix I must say though. For someone like myself, not comming from the world of C, the sprintf function often seams like magic. This time all that was needed was a 10 sec. look at the documentation. So it goes!
Author
Owner

@Seldaek commented on GitHub (Apr 23, 2012):

Ok cool!

<!-- gh-comment-id:5278015 --> @Seldaek commented on GitHub (Apr 23, 2012): Ok cool!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#16
No description provided.