From 4a43d9b17c33bc889674f0fe6c8dc25a2b9b4f73 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 14 Nov 2016 11:53:15 +0100 Subject: [PATCH] Fix microseconds support on 7.1, and enable it by default as 7.1 has no perf cost anymore --- src/Monolog/DateTimeImmutable.php | 4 +++- src/Monolog/Logger.php | 8 ++++++-- tests/Monolog/Handler/RavenHandlerTest.php | 4 ++-- tests/Monolog/LoggerTest.php | 8 +++++--- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Monolog/DateTimeImmutable.php b/src/Monolog/DateTimeImmutable.php index 45b80c7c..4e9f598e 100644 --- a/src/Monolog/DateTimeImmutable.php +++ b/src/Monolog/DateTimeImmutable.php @@ -23,10 +23,12 @@ class DateTimeImmutable extends \DateTimeImmutable implements \JsonSerializable public function __construct($useMicroseconds, \DateTimeZone $timezone = null) { + static $needsMicrosecondsHack = PHP_VERSION_ID < 70100; + $this->useMicroseconds = $useMicroseconds; $date = 'now'; - if ($useMicroseconds) { + if ($needsMicrosecondsHack && $useMicroseconds) { $timestamp = microtime(true); // apply offset of the timezone as microtime() is always UTC diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 438c770f..8d63cc7b 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -128,7 +128,7 @@ class Logger implements LoggerInterface /** * @var bool */ - protected $microsecondTimestamps = false; + protected $microsecondTimestamps = true; /** * @var DateTimeZone @@ -251,13 +251,17 @@ class Logger implements LoggerInterface * Control the use of microsecond resolution timestamps in the 'datetime' * member of new records. * - * Generating microsecond resolution timestamps by calling + * On PHP7.0, generating microsecond resolution timestamps by calling * microtime(true), formatting the result via sprintf() and then parsing * the resulting string via \DateTime::createFromFormat() can incur * a measurable runtime overhead vs simple usage of DateTime to capture * a second resolution timestamp in systems which generate a large number * of log events. * + * On PHP7.1 however microseconds are always included by the engine, so + * this setting can be left alone unless you really want to suppress + * microseconds in the output. + * * @param bool $micro True to use microtime() to create timestamps */ public function useMicrosecondTimestamps(bool $micro) diff --git a/tests/Monolog/Handler/RavenHandlerTest.php b/tests/Monolog/Handler/RavenHandlerTest.php index f9ab51b1..23a63ee7 100644 --- a/tests/Monolog/Handler/RavenHandlerTest.php +++ b/tests/Monolog/Handler/RavenHandlerTest.php @@ -215,10 +215,10 @@ class RavenHandlerTest extends TestCase $this->getRecord(Logger::INFO, 'information 2'), ); - $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface'); + $logFormatter = $this->createMock('Monolog\\Formatter\\FormatterInterface'); $logFormatter->expects($this->once())->method('formatBatch'); - $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface'); + $formatter = $this->createMock('Monolog\\Formatter\\FormatterInterface'); $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) use ($records) { return $record['message'] == 'error 1'; })); diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index e2adec1b..e167cc6c 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -551,7 +551,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase * @covers Monolog\Logger::useMicrosecondTimestamps * @covers Monolog\Logger::addRecord */ - public function testUseMicrosecondTimestamps($micro, $assert) + public function testUseMicrosecondTimestamps($micro, $assert, $assertFormat) { $logger = new Logger('foo'); $logger->useMicrosecondTimestamps($micro); @@ -560,14 +560,16 @@ class LoggerTest extends \PHPUnit_Framework_TestCase $logger->info('test'); list($record) = $handler->getRecords(); $this->{$assert}('000000', $record['datetime']->format('u')); + $this->assertSame($record['datetime']->format($assertFormat), (string) $record['datetime']); } public function useMicrosecondTimestampsProvider() { return [ // this has a very small chance of a false negative (1/10^6) - 'with microseconds' => [true, 'assertNotSame'], - 'without microseconds' => [false, 'assertSame'], + 'with microseconds' => [true, 'assertNotSame', 'Y-m-d\TH:i:s.uP'], + // php 7.1 always includes microseconds, so we keep them in, but we format the datetime without + 'without microseconds' => [false, PHP_VERSION_ID >= 70100 ? 'assertNotSame' : 'assertSame', 'Y-m-d\TH:i:sP'], ]; } }