1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-19 23:56:17 +02:00

Support ext-mongodb's UTCDateTime class in MongoDBFormatter

The legacy driver (i.e. ext-mongo) and MongoDate are not supported on PHP 7.
This commit is contained in:
Jeremy Mikola
2016-01-21 16:22:47 -05:00
parent aa6ab660bd
commit f585e714fc
2 changed files with 24 additions and 14 deletions

View File

@@ -11,6 +11,8 @@
namespace Monolog\Formatter;
use MongoDB\BSON\UTCDateTime;
/**
* Formats a record for use with the MongoDBHandler.
*
@@ -100,6 +102,13 @@ class MongoDBFormatter implements FormatterInterface
protected function formatDate(\DateTime $value, $nestingLevel)
{
return new \MongoDate($value->getTimestamp());
$seconds = (int) $value->format('U');
$milliseconds = (int) $value->format('u') / 1000;
if ($seconds < 0) {
return new UTCDateTime($seconds * 1000 - $milliseconds);
} else {
return new UTCDateTime($seconds * 1000 + $milliseconds);
}
}
}