Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"graylog2/gelf-php": "^1.4.2 || ^2.0",
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.2",
"mongodb/mongodb": "^1.8",
"mongodb/mongodb": "^1.21 || ^2.0",
"php-amqplib/php-amqplib": "~2.4 || ^3",
"php-console/php-console": "^3.1.8",
"phpstan/phpstan": "^2",
Expand Down
30 changes: 0 additions & 30 deletions src/Monolog/Formatter/MongoDBFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class MongoDBFormatter implements FormatterInterface
{
private bool $exceptionTraceAsString;
private int $maxNestingLevel;
private bool $isLegacyMongoExt;

/**
* @param int $maxNestingLevel 0 means infinite nesting, the $record itself is level 1, $record->context is 2
Expand All @@ -35,8 +34,6 @@ public function __construct(int $maxNestingLevel = 3, bool $exceptionTraceAsStri
{
$this->maxNestingLevel = max($maxNestingLevel, 0);
$this->exceptionTraceAsString = $exceptionTraceAsString;

$this->isLegacyMongoExt = \extension_loaded('mongodb') && version_compare((string) phpversion('mongodb'), '1.1.9', '<=');
}

/**
Expand Down Expand Up @@ -126,34 +123,7 @@ protected function formatException(\Throwable $exception, int $nestingLevel)
}

protected function formatDate(\DateTimeInterface $value, int $nestingLevel): UTCDateTime
{
if ($this->isLegacyMongoExt) {
return $this->legacyGetMongoDbDateTime($value);
}

return $this->getMongoDbDateTime($value);
}

private function getMongoDbDateTime(\DateTimeInterface $value): UTCDateTime
{
return new UTCDateTime((int) floor(((float) $value->format('U.u')) * 1000));
}

/**
* This is needed to support MongoDB Driver v1.19 and below
*
* See https://github.com/mongodb/mongo-php-driver/issues/426
*
* It can probably be removed in 2.1 or later once MongoDB's 1.2 is released and widely adopted
*/
private function legacyGetMongoDbDateTime(\DateTimeInterface $value): UTCDateTime
{
$milliseconds = floor(((float) $value->format('U.u')) * 1000);

$milliseconds = (PHP_INT_SIZE === 8) //64-bit OS?
? (int) $milliseconds
: (string) $milliseconds;

return new UTCDateTime($milliseconds);
}
}
7 changes: 4 additions & 3 deletions src/Monolog/Handler/MongoDBHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

namespace Monolog\Handler;

use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Manager;
use MongoDB\Client;
use Monolog\Level;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\MongoDBFormatter;
Expand All @@ -34,7 +35,7 @@
*/
class MongoDBHandler extends AbstractProcessingHandler
{
private \MongoDB\Collection $collection;
private Collection $collection;

private Client|Manager $manager;

Expand All @@ -50,7 +51,7 @@ class MongoDBHandler extends AbstractProcessingHandler
public function __construct(Client|Manager $mongodb, string $database, string $collection, int|string|Level $level = Level::Debug, bool $bubble = true)
{
if ($mongodb instanceof Client) {
$this->collection = $mongodb->selectCollection($database, $collection);
$this->collection = $mongodb->getCollection($database, $collection);
} else {
$this->manager = $mongodb;
$this->namespace = $database . '.' . $collection;
Expand Down
6 changes: 3 additions & 3 deletions tests/Monolog/Formatter/MongoDBFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MongoDBFormatterTest extends \Monolog\Test\MonologTestCase
{
public function setUp(): void
{
if (!class_exists('MongoDB\BSON\UTCDateTime')) {
if (!class_exists(UTCDateTime::class)) {
$this->markTestSkipped('ext-mongodb not installed');
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testSimpleFormat()
$this->assertEquals(Level::Warning->value, $formattedRecord['level']);
$this->assertEquals(Level::Warning->getName(), $formattedRecord['level_name']);
$this->assertEquals('test', $formattedRecord['channel']);
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $formattedRecord['datetime']);
$this->assertInstanceOf(UTCDateTime::class, $formattedRecord['datetime']);
$this->assertEquals('1453410690123', $formattedRecord['datetime']->__toString());
$this->assertEquals([], $formattedRecord['extra']);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public function testRecursiveFormat()
$formattedRecord = $formatter->format($record);

$this->assertCount(5, $formattedRecord['context']);
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $formattedRecord['context']['stuff']);
$this->assertInstanceOf(UTCDateTime::class, $formattedRecord['context']['stuff']);
$this->assertEquals('-29731710213', $formattedRecord['context']['stuff']->__toString());
$this->assertEquals(
[
Expand Down
10 changes: 6 additions & 4 deletions tests/Monolog/Handler/MongoDBHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Monolog\Handler;

use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Driver\Manager;

/**
Expand All @@ -27,20 +29,20 @@ public function testConstructorShouldThrowExceptionForInvalidMongo()

public function testHandleWithLibraryClient()
{
if (!(class_exists('MongoDB\Client'))) {
if (!class_exists(Client::class)) {
$this->markTestSkipped('mongodb/mongodb not installed');
}

$mongodb = $this->getMockBuilder('MongoDB\Client')
$mongodb = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();

$collection = $this->getMockBuilder('MongoDB\Collection')
$collection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();

$mongodb->expects($this->once())
->method('selectCollection')
->method('getCollection')
->with('db', 'collection')
->willReturn($collection);

Expand Down
Loading