1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-11 23:54:04 +02:00

MongoDBHandler only supports ext-mongodb and mongodb/mongodb

The legacy driver (i.e. ext-mongo) is not supported on PHP 7, so there is no reason to leave behind its supporting code.
This commit is contained in:
Jeremy Mikola
2016-01-21 15:58:45 -05:00
parent d860b763cb
commit aa6ab660bd
3 changed files with 70 additions and 102 deletions

View File

@@ -35,8 +35,8 @@
"ruflin/elastica": "Allow sending log messages to an Elastic Search server", "ruflin/elastica": "Allow sending log messages to an Elastic Search server",
"videlalvaro/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", "videlalvaro/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
"ext-mongo": "Allow sending log messages to a MongoDB server", "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)",
"mongodb/mongodb": "Allow sending log messages to a MongoDB server via PHP Driver", "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)",
"aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
"rollbar/rollbar": "Allow sending log messages to Rollbar", "rollbar/rollbar": "Allow sending log messages to Rollbar",
"php-console/php-console": "Allow sending log messages to Google Chrome" "php-console/php-console": "Allow sending log messages to Google Chrome"

View File

@@ -11,55 +11,51 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Logger; use MongoDB\Driver\BulkWrite;
use Monolog\Formatter\NormalizerFormatter;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Client; use MongoDB\Client;
//use Mongo; use Monolog\Logger;
use MongoClient; use Monolog\Formatter\NormalizerFormatter;
/** /**
* Logs to a MongoDB database. * Logs to a MongoDB database.
* *
* usage example: * Usage example:
* *
* $log = new Logger('application'); * $log = new \Monolog\Logger('application');
* $mongodb = new MongoDBHandler(new \Mongo("mongodb://localhost:27017"), "logs", "prod"); * $client = new \MongoDB\Client('mongodb://localhost:27017');
* $mongodb = new \Monolog\Handler\MongoDBHandler($client, 'logs', 'prod');
* $log->pushHandler($mongodb); * $log->pushHandler($mongodb);
* *
* The above examples uses the MongoDB PHP library's client class; however, * The above examples uses the MongoDB PHP library's client class; however, the
* classes from ext-mongodb (MongoDB\Driver\Manager) and ext-mongo (Mongo and * MongoDB\Driver\Manager class from ext-mongodb is also supported.
* MongoClient) are also supported.
*
* @author Thomas Tourlourat <thomas@tourlourat.com>
*/ */
class MongoDBHandler extends AbstractProcessingHandler class MongoDBHandler extends AbstractProcessingHandler
{ {
private $mongoCollection; private $collection;
private $namespace;
private $manager; private $manager;
private $namespace;
/** /**
* Constructor. * Constructor.
* *
* @param Client|Manager|Mongo|MongoClient $mongo MongoDB driver or library instance * @param Client|Manager $mongodb MongoDB library or driver client
* @param string $database Database name * @param string $database Database name
* @param string $collection Collection name * @param string $collection Collection name
* @param int $level The minimum logging level at which this handler will be triggered * @param int $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/ */
public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) public function __construct($mongodb, $database, $collection, $level = Logger::DEBUG, $bubble = true)
{ {
if (!($mongo instanceof MongoClient || $mongo instanceof \Mongo || $mongo instanceof MongoDB\Client || $mongo instanceof Manager)) { if (!($mongodb instanceof Client || $mongodb instanceof Manager)) {
throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required'); throw new \InvalidArgumentException('MongoDB\Client or MongoDB\Driver\Manager instance required');
} }
if ($mongo instanceof Manger) { if ($mongodb instanceof Client) {
$this->manager = $mongo; $this->collection = $mongodb->selectCollection($database, $collection);
$this->namespace = $database . '.' . $collection;
} else { } else {
$this->mongoCollection = $mongo->selectCollection($database, $collection); $this->manager = $mongodb;
$this->namespace = $database . '.' . $collection;
} }
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
@@ -67,23 +63,15 @@ class MongoDBHandler extends AbstractProcessingHandler
protected function write(array $record) protected function write(array $record)
{ {
if ($this->mongoCollection instanceof Collection) { if (isset($this->collection)) {
$this->mongoCollection->insertOne($record["formatted"]); $this->collection->insertOne($record['formatted']);
return;
} }
if ($this->mongoCollection instanceof MongoCollection) { if (isset($this->manager, $this->namespace)) {
$this->mongoCollection->insert($record["formatted"]); $bulk = new BulkWrite;
$bulk->insert($record["formatted"]);
return; $this->manager->executeBulkWrite($this->namespace, $bulk);
} }
// $this->manager instanceof \MongoDB\Driver\Manager
$bulk = new BulkWrite();
$bulk->insert($record["formatted"]);
$this->$manager->executeBulkWrite($this->namespace, $bulk);
} }
/** /**
@@ -91,6 +79,6 @@ class MongoDBHandler extends AbstractProcessingHandler
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter()
{ {
return new NormalizerFormatter(); return new NormalizerFormatter;
} }
} }

View File

@@ -11,8 +11,9 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use MongoDB\Driver\Manager;
use Monolog\TestCase; use Monolog\TestCase;
use Monolog\Logger; use Monolog\Formatter\NormalizerFormatter;
class MongoDBHandlerTest extends TestCase class MongoDBHandlerTest extends TestCase
{ {
@@ -21,78 +22,57 @@ class MongoDBHandlerTest extends TestCase
*/ */
public function testConstructorShouldThrowExceptionForInvalidMongo() public function testConstructorShouldThrowExceptionForInvalidMongo()
{ {
new MongoDBHandler(new \stdClass(), 'DB', 'Collection'); new MongoDBHandler(new \stdClass, 'db', 'collection');
} }
public function testHandle() public function testHandleWithLibraryClient()
{ {
$mongo = $this->getMock('Mongo', array('selectCollection'), array(), '', false); if (!(class_exists('MongoDB\Client'))) {
$collection = $this->getMock('stdClass', array('insert')); $this->markTestSkipped('mongodb/mongodb not installed');
}
$mongo->expects($this->once()) $mongodb = $this->getMockBuilder('MongoDB\Client')
->disableOriginalConstructor()
->getMock();
$collection = $this->getMockBuilder('MongoDB\Collection')
->disableOriginalConstructor()
->getMock();
$mongodb->expects($this->once())
->method('selectCollection') ->method('selectCollection')
->with('DB', 'Collection') ->with('db', 'collection')
->will($this->returnValue($collection)); ->will($this->returnValue($collection));
$record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34)); $record = $this->getRecord();
$expected = $record;
$expected = array( $expected['datetime'] = $record['datetime']->format(NormalizerFormatter::SIMPLE_DATE);
'message' => 'test',
'context' => array('data' => '[object] (stdClass: {})', 'foo' => 34),
'level' => Logger::WARNING,
'level_name' => 'WARNING',
'channel' => 'test',
'datetime' => $record['datetime']->format('Y-m-d H:i:s'),
'extra' => array(),
);
$collection->expects($this->once()) $collection->expects($this->once())
->method('insert') ->method('insertOne')
->with($expected); ->with($expected);
$handler = new MongoDBHandler($mongo, 'DB', 'Collection'); $handler = new MongoDBHandler($mongodb, 'db', 'collection');
$handler->handle($record); $handler->handle($record);
} }
public function testHandleWithManager() { public function testHandleWithDriverManager()
if (!(class_exists('MongoDB\Driver\Manager'))) {
$this->markTestSkipped('mongo extension not installed');
}
$manager = $this->getMock('MongoDB\Driver\Manager', array('executeBulkWrite'), array(), '', false);
$record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34));
$expected = array(
'message' => 'test',
'context' => array('data' => '[object] (stdClass: {})', 'foo' => 34),
'level' => Logger::WARNING,
'level_name' => 'WARNING',
'channel' => 'test',
'datetime' => $record['datetime']->format('Y-m-d H:i:s'),
'extra' => array(),
);
$bulk = new \MongoDB\Driver\BulkWrite();
$bulk->insert($expected);
$manager->expects($this->once())
->method('executeBulkWrite')
->with('DB.Collection', $bulk);
$handler = new MongoDBHandler($manager, 'DB', 'Collection');
$handler->handle($record);
}
}
if (!class_exists('Mongo')) {
class Mongo
{ {
public function selectCollection() if (!(class_exists('MongoDB\Driver\Manager'))) {
{ $this->markTestSkipped('ext-mongodb not installed');
}
/* This can become a unit test once ManagerInterface can be mocked.
* See: https://jira.mongodb.org/browse/PHPC-378
*/
$mongodb = new Manager('mongodb://localhost:27017');
$handler = new MongoDBHandler($mongodb, 'test', 'monolog');
$record = $this->getRecord();
try {
$handler->handle($record);
} catch (\RuntimeException $e) {
$this->markTestSkipped('Could not connect to MongoDB server on mongodb://localhost:27017');
} }
} }
} }