1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-22 09:06:10 +02:00

Response to CR comments

This commit is contained in:
Will Banfield
2016-01-20 16:21:36 -05:00
parent f6a9fdbb2c
commit d860b763cb

View File

@@ -13,6 +13,10 @@ namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\NormalizerFormatter; use Monolog\Formatter\NormalizerFormatter;
use MongoDB\Driver\Manager;
use MongoDB\Client;
//use Mongo;
use MongoClient;
/** /**
* Logs to a MongoDB database. * Logs to a MongoDB database.
@@ -23,22 +27,37 @@ use Monolog\Formatter\NormalizerFormatter;
* $mongodb = new MongoDBHandler(new \Mongo("mongodb://localhost:27017"), "logs", "prod"); * $mongodb = new MongoDBHandler(new \Mongo("mongodb://localhost:27017"), "logs", "prod");
* $log->pushHandler($mongodb); * $log->pushHandler($mongodb);
* *
* The above examples uses the MongoDB PHP library's client class; however,
* classes from ext-mongodb (MongoDB\Driver\Manager) and ext-mongo (Mongo and
* MongoClient) are also supported.
*
* @author Thomas Tourlourat <thomas@tourlourat.com> * @author Thomas Tourlourat <thomas@tourlourat.com>
*/ */
class MongoDBHandler extends AbstractProcessingHandler class MongoDBHandler extends AbstractProcessingHandler
{ {
protected $mongoCollection; private $mongoCollection;
protected $namespace; private $namespace;
protected $manager; private $manager;
/**
* Constructor.
*
* @param Client|Manager|Mongo|MongoClient $mongo MongoDB driver or library instance
* @param string $database Database name
* @param string $collection Collection name
* @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
*/
public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true)
{ {
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client || $mongo instanceof \MongoDB\Driver\Manager)) { if (!($mongo instanceof MongoClient || $mongo instanceof \Mongo || $mongo instanceof MongoDB\Client || $mongo instanceof Manager)) {
throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required'); throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required');
} }
$this->namespace = "$database.$collection";
if($mongo instanceof \MongoDB\Driver\Manger) { if ($mongo instanceof Manger) {
$this->manager = $mongo; $this->manager = $mongo;
$this->namespace = $database . '.' . $collection;
} else { } else {
$this->mongoCollection = $mongo->selectCollection($database, $collection); $this->mongoCollection = $mongo->selectCollection($database, $collection);
} }
@@ -48,15 +67,23 @@ class MongoDBHandler extends AbstractProcessingHandler
protected function write(array $record) protected function write(array $record)
{ {
if ($this->mongoCollection instanceof \MongoDB\Collection) { if ($this->mongoCollection instanceof Collection) {
$this->mongoCollection->insertOne($record["formatted"]); $this->mongoCollection->insertOne($record["formatted"]);
} else if($this->mongoCollection instanceof \MongoCollection) {
return;
}
if ($this->mongoCollection instanceof MongoCollection) {
$this->mongoCollection->insert($record["formatted"]); $this->mongoCollection->insert($record["formatted"]);
} else {
$bulk = new \MongoDB\Driver\BulkWrite(); return;
}
// $this->manager instanceof \MongoDB\Driver\Manager
$bulk = new BulkWrite();
$bulk->insert($record["formatted"]); $bulk->insert($record["formatted"]);
$this->$manager->executeBulkWrite($this->namespace, $bulk); $this->$manager->executeBulkWrite($this->namespace, $bulk);
}
} }
/** /**