scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of the Monolog package. |
||
5 | * |
||
6 | * (c) Jordi Boggiano <j.boggiano@seld.be> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Monolog\Handler; |
||
13 | |||
14 | use Monolog\Logger; |
||
15 | use Monolog\Formatter\NormalizerFormatter; |
||
16 | |||
17 | /** |
||
18 | * Logs to a MongoDB database. |
||
19 | * |
||
20 | * usage example: |
||
21 | * |
||
22 | * $log = new Logger('application'); |
||
23 | * $mongodb = new MongoDBHandler(new \Mongo("mongodb://localhost:27017"), "logs", "prod"); |
||
24 | * $log->pushHandler($mongodb); |
||
25 | * |
||
26 | * @author Thomas Tourlourat <thomas@tourlourat.com> |
||
27 | */ |
||
28 | class MongoDBHandler extends AbstractProcessingHandler |
||
29 | { |
||
30 | protected $mongoCollection; |
||
31 | |||
32 | public function __construct($mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) |
||
33 | { |
||
34 | if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client)) { |
||
35 | throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB\Client instance required'); |
||
36 | } |
||
37 | |||
38 | $this->mongoCollection = $mongo->selectCollection($database, $collection); |
||
39 | |||
40 | parent::__construct($level, $bubble); |
||
41 | } |
||
42 | |||
43 | protected function write(array $record) |
||
44 | { |
||
45 | if ($this->mongoCollection instanceof \MongoDB\Collection) { |
||
46 | $this->mongoCollection->insertOne($record["formatted"]); |
||
47 | } else { |
||
48 | $this->mongoCollection->save($record["formatted"]); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritDoc} |
||
54 | */ |
||
55 | protected function getDefaultFormatter() |
||
56 | { |
||
57 | return new NormalizerFormatter(); |
||
58 | } |
||
59 | } |