1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-log for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Log\Writer;
use DateTimeInterface;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Manager;
use MongoDB\Driver\WriteConcern;
use MongoDB\BSON\UTCDateTime;
use Traversable;
use Zend\Log\Exception;
use Zend\Log\Formatter\FormatterInterface;
use Zend\Stdlib\ArrayUtils;
/**
* MongoDB log writer.
*/
class MongoDB extends AbstractWriter
{
/**
* @var Manager
*/
protected $manager;
/**
* @var string
*/
protected $database;
/**
* @var WriteConcern
*/
protected $writeConcern;
/**
* Constructor
*
* @param Manager|array|Traversable $manager
* @param string $database
* @param string $collection
* @param WriteConcern|array|Traversable $writeConcern
* @throws Exception\InvalidArgumentException
*/
public function __construct($manager, $database = null, $collection = null, $writeConcern = null)
{
if (! extension_loaded('mongodb')) {
throw new Exception\ExtensionNotLoadedException('Missing ext/mongodb');
}
if ($manager instanceof Traversable) {
// Configuration may be multi-dimensional due to save options
$manager = ArrayUtils::iteratorToArray($manager);
}
if (is_array($manager)) {
parent::__construct($manager);
$writeConcern = isset($manager['write_concern']) ? $manager['write_concern'] : new WriteConcern(1);
$collection = isset($manager['collection']) ? $manager['collection'] : null;
$database = isset($manager['database']) ? $manager['database'] : null;
$manager = isset($manager['manager']) ? $manager['manager'] : null;
}
if (null === $database) {
throw new Exception\InvalidArgumentException('The database parameter cannot be empty');
}
if (null !== $collection) {
$database = sprintf('%s.%s', $database, $collection);
}
if (! $manager instanceof Manager) {
throw new Exception\InvalidArgumentException(sprintf(
'Parameter of type %s is invalid; must be MongoDB\Driver\Manager',
(is_object($manager) ? get_class($manager) : gettype($manager))
));
}
if ($writeConcern instanceof Traversable) {
$writeConcern = iterator_to_array($writeConcern);
}
if (is_array($writeConcern)) {
$wstring = isset($writeConcern['wstring']) ? $writeConcern['wstring'] : 1;
$wtimeout = isset($writeConcern['wtimeout']) ? $writeConcern['wtimeout'] : 0;
$journal = isset($writeConcern['journal']) ? $writeConcern['journal'] : false;
$writeConcern = new WriteConcern($wstring, $wtimeout, $journal);
}
$this->manager = $manager;
$this->database = $database;
$this->writeConcern = $writeConcern;
}
/**
* This writer does not support formatting.
*
* @param string|FormatterInterface $formatter
* @param array|null $options (unused)
* @return WriterInterface
*/
public function setFormatter($formatter, array $options = null)
{
return $this;
}
/**
* Write a message to the log.
*
* @param array $event Event data
* @return void
* @throws Exception\RuntimeException
*/
protected function doWrite(array $event)
{
if (null === $this->manager) {
throw new Exception\RuntimeException('MongoDB\Driver\Manager must be defined');
}
if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTimeInterface) {
$millis = (int) floor((float) $event['timestamp']->format('U.u') * 1000);
$event['timestamp'] = new UTCDateTime($millis);
}
$bulkWrite = new BulkWrite();
$bulkWrite->insert($event);
$this->manager->executeBulkWrite($this->database, $bulkWrite, $this->writeConcern);
}
}