1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 20:57:36 +02:00

Fix microseconds support on 7.1, and enable it by default as 7.1 has no perf cost anymore

This commit is contained in:
Jordi Boggiano
2016-11-14 11:53:15 +01:00
parent 9691bde77a
commit 4a43d9b17c
4 changed files with 16 additions and 8 deletions

View File

@@ -23,10 +23,12 @@ class DateTimeImmutable extends \DateTimeImmutable implements \JsonSerializable
public function __construct($useMicroseconds, \DateTimeZone $timezone = null) public function __construct($useMicroseconds, \DateTimeZone $timezone = null)
{ {
static $needsMicrosecondsHack = PHP_VERSION_ID < 70100;
$this->useMicroseconds = $useMicroseconds; $this->useMicroseconds = $useMicroseconds;
$date = 'now'; $date = 'now';
if ($useMicroseconds) { if ($needsMicrosecondsHack && $useMicroseconds) {
$timestamp = microtime(true); $timestamp = microtime(true);
// apply offset of the timezone as microtime() is always UTC // apply offset of the timezone as microtime() is always UTC

View File

@@ -128,7 +128,7 @@ class Logger implements LoggerInterface
/** /**
* @var bool * @var bool
*/ */
protected $microsecondTimestamps = false; protected $microsecondTimestamps = true;
/** /**
* @var DateTimeZone * @var DateTimeZone
@@ -251,13 +251,17 @@ class Logger implements LoggerInterface
* Control the use of microsecond resolution timestamps in the 'datetime' * Control the use of microsecond resolution timestamps in the 'datetime'
* member of new records. * 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 * microtime(true), formatting the result via sprintf() and then parsing
* the resulting string via \DateTime::createFromFormat() can incur * the resulting string via \DateTime::createFromFormat() can incur
* a measurable runtime overhead vs simple usage of DateTime to capture * a measurable runtime overhead vs simple usage of DateTime to capture
* a second resolution timestamp in systems which generate a large number * a second resolution timestamp in systems which generate a large number
* of log events. * 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 * @param bool $micro True to use microtime() to create timestamps
*/ */
public function useMicrosecondTimestamps(bool $micro) public function useMicrosecondTimestamps(bool $micro)

View File

@@ -215,10 +215,10 @@ class RavenHandlerTest extends TestCase
$this->getRecord(Logger::INFO, 'information 2'), $this->getRecord(Logger::INFO, 'information 2'),
); );
$logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface'); $logFormatter = $this->createMock('Monolog\\Formatter\\FormatterInterface');
$logFormatter->expects($this->once())->method('formatBatch'); $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) { $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) use ($records) {
return $record['message'] == 'error 1'; return $record['message'] == 'error 1';
})); }));

View File

@@ -551,7 +551,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
* @covers Monolog\Logger::useMicrosecondTimestamps * @covers Monolog\Logger::useMicrosecondTimestamps
* @covers Monolog\Logger::addRecord * @covers Monolog\Logger::addRecord
*/ */
public function testUseMicrosecondTimestamps($micro, $assert) public function testUseMicrosecondTimestamps($micro, $assert, $assertFormat)
{ {
$logger = new Logger('foo'); $logger = new Logger('foo');
$logger->useMicrosecondTimestamps($micro); $logger->useMicrosecondTimestamps($micro);
@@ -560,14 +560,16 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->info('test'); $logger->info('test');
list($record) = $handler->getRecords(); list($record) = $handler->getRecords();
$this->{$assert}('000000', $record['datetime']->format('u')); $this->{$assert}('000000', $record['datetime']->format('u'));
$this->assertSame($record['datetime']->format($assertFormat), (string) $record['datetime']);
} }
public function useMicrosecondTimestampsProvider() public function useMicrosecondTimestampsProvider()
{ {
return [ return [
// this has a very small chance of a false negative (1/10^6) // this has a very small chance of a false negative (1/10^6)
'with microseconds' => [true, 'assertNotSame'], 'with microseconds' => [true, 'assertNotSame', 'Y-m-d\TH:i:s.uP'],
'without microseconds' => [false, 'assertSame'], // 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'],
]; ];
} }
} }