PluginProbe
Media Cloud Sync / 1.2.5
Media Cloud Sync v1.2.5
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / monolog / monolog / src / Monolog / Handler / MongoDBHandler.php

MongoDBHandler.php in Media Cloud Sync 1.2.5, at includes/sdk/google/monolog/monolog/src/Monolog/Handler/MongoDBHandler.php

80 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 /*
5 * This file is part of the Monolog package.
6 *
7 * (c) Jordi Boggiano <[email protected]>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12 namespace Dudlewebs\WPMCS\Monolog\Handler;
13
14 use MongoDB\Driver\BulkWrite;
15 use MongoDB\Driver\Manager;
16 use Dudlewebs\WPMCS\MongoDB\Client;
17 use Dudlewebs\WPMCS\Monolog\Logger;
18 use Dudlewebs\WPMCS\Monolog\Formatter\FormatterInterface;
19 use Dudlewebs\WPMCS\Monolog\Formatter\MongoDBFormatter;
20 /**
21 * Logs to a MongoDB database.
22 *
23 * Usage example:
24 *
25 * $log = new \Monolog\Logger('application');
26 * $client = new \MongoDB\Client('mongodb://localhost:27017');
27 * $mongodb = new \Monolog\Handler\MongoDBHandler($client, 'logs', 'prod');
28 * $log->pushHandler($mongodb);
29 *
30 * The above examples uses the MongoDB PHP library's client class; however, the
31 * MongoDB\Driver\Manager class from ext-mongodb is also supported.
32 */
33 class MongoDBHandler extends AbstractProcessingHandler
34 {
35 /** @var \MongoDB\Collection */
36 private $collection;
37 /** @var Client|Manager */
38 private $manager;
39 /** @var string */
40 private $namespace;
41 /**
42 * Constructor.
43 *
44 * @param Client|Manager $mongodb MongoDB library or driver client
45 * @param string $database Database name
46 * @param string $collection Collection name
47 */
48 public function __construct($mongodb, string $database, string $collection, $level = Logger::DEBUG, bool $bubble = \true)
49 {
50 if (!($mongodb instanceof Client || $mongodb instanceof Manager)) {
51 throw new \InvalidArgumentException('MongoDB\Client or MongoDB\Driver\Manager instance required');
52 }
53 if ($mongodb instanceof Client) {
54 $this->collection = $mongodb->selectCollection($database, $collection);
55 } else {
56 $this->manager = $mongodb;
57 $this->namespace = $database . '.' . $collection;
58 }
59 parent::__construct($level, $bubble);
60 }
61 protected function write(array $record): void
62 {
63 if (isset($this->collection)) {
64 $this->collection->insertOne($record['formatted']);
65 }
66 if (isset($this->manager, $this->namespace)) {
67 $bulk = new BulkWrite();
68 $bulk->insert($record["formatted"]);
69 $this->manager->executeBulkWrite($this->namespace, $bulk);
70 }
71 }
72 /**
73 * {@inheritDoc}
74 */
75 protected function getDefaultFormatter(): FormatterInterface
76 {
77 return new MongoDBFormatter();
78 }
79 }
80