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

Merge pull request #724 from billybanfield/ISSUE-695

Support ext-mongodb in MongoDBHandler
This commit is contained in:
Jordi Boggiano
2016-03-01 17:04:31 +00:00
5 changed files with 107 additions and 59 deletions

View File

@@ -11,41 +11,66 @@
namespace Monolog\Handler;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Manager;
use MongoDB\Client;
use Monolog\Logger;
use Monolog\Formatter\NormalizerFormatter;
/**
* Logs to a MongoDB database.
*
* usage example:
* Usage example:
*
* $log = new Logger('application');
* $mongodb = new MongoDBHandler(new \Mongo("mongodb://localhost:27017"), "logs", "prod");
* $log = new \Monolog\Logger('application');
* $client = new \MongoDB\Client('mongodb://localhost:27017');
* $mongodb = new \Monolog\Handler\MongoDBHandler($client, 'logs', 'prod');
* $log->pushHandler($mongodb);
*
* @author Thomas Tourlourat <thomas@tourlourat.com>
* The above examples uses the MongoDB PHP library's client class; however, the
* MongoDB\Driver\Manager class from ext-mongodb is also supported.
*/
class MongoDBHandler extends AbstractProcessingHandler
{
protected $mongoCollection;
private $collection;
private $manager;
private $namespace;
public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true)
/**
* Constructor.
*
* @param Client|Manager $mongodb MongoDB library or driver client
* @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($mongodb, $database, $collection, $level = Logger::DEBUG, $bubble = true)
{
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client)) {
throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required');
if (!($mongodb instanceof Client || $mongodb instanceof Manager)) {
throw new \InvalidArgumentException('MongoDB\Client or MongoDB\Driver\Manager instance required');
}
$this->mongoCollection = $mongo->selectCollection($database, $collection);
if ($mongodb instanceof Client) {
$this->collection = $mongodb->selectCollection($database, $collection);
} else {
$this->manager = $mongodb;
$this->namespace = $database . '.' . $collection;
}
parent::__construct($level, $bubble);
}
protected function write(array $record)
{
if ($this->mongoCollection instanceof \MongoDB\Collection) {
$this->mongoCollection->insertOne($record["formatted"]);
} else {
$this->mongoCollection->save($record["formatted"]);
if (isset($this->collection)) {
$this->collection->insertOne($record['formatted']);
}
if (isset($this->manager, $this->namespace)) {
$bulk = new BulkWrite;
$bulk->insert($record["formatted"]);
$this->manager->executeBulkWrite($this->namespace, $bulk);
}
}
@@ -54,6 +79,6 @@ class MongoDBHandler extends AbstractProcessingHandler
*/
protected function getDefaultFormatter()
{
return new NormalizerFormatter();
return new NormalizerFormatter;
}
}