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:
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace Monolog\Formatter;
|
||||
|
||||
use MongoDB\BSON\UTCDateTime;
|
||||
|
||||
/**
|
||||
* Formats a record for use with the MongoDBHandler.
|
||||
*
|
||||
@@ -55,9 +57,9 @@ class MongoDBFormatter implements FormatterInterface
|
||||
{
|
||||
if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) {
|
||||
foreach ($record as $name => $value) {
|
||||
if ($value instanceof \DateTime) {
|
||||
if ($value instanceof \DateTimeInterface) {
|
||||
$record[$name] = $this->formatDate($value, $nestingLevel + 1);
|
||||
} elseif ($value instanceof \Exception) {
|
||||
} elseif ($value instanceof \Throwable) {
|
||||
$record[$name] = $this->formatException($value, $nestingLevel + 1);
|
||||
} elseif (is_array($value)) {
|
||||
$record[$name] = $this->formatArray($value, $nestingLevel + 1);
|
||||
@@ -80,7 +82,7 @@ class MongoDBFormatter implements FormatterInterface
|
||||
return $this->formatArray($objectVars, $nestingLevel);
|
||||
}
|
||||
|
||||
protected function formatException(\Exception $exception, $nestingLevel)
|
||||
protected function formatException(\Throwable $exception, $nestingLevel)
|
||||
{
|
||||
$formattedException = array(
|
||||
'class' => get_class($exception),
|
||||
@@ -98,8 +100,15 @@ class MongoDBFormatter implements FormatterInterface
|
||||
return $this->formatArray($formattedException, $nestingLevel);
|
||||
}
|
||||
|
||||
protected function formatDate(\DateTime $value, $nestingLevel)
|
||||
protected function formatDate(\DateTimeInterface $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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user