scratch – Blame information for rev

Subversion Repositories:
Rev:
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\Formatter\FormatterInterface;
15 use Monolog\Formatter\ElasticaFormatter;
16 use Monolog\Logger;
17 use Elastica\Client;
18 use Elastica\Exception\ExceptionInterface;
19  
20 /**
21 * Elastic Search handler
22 *
23 * Usage example:
24 *
25 * $client = new \Elastica\Client();
26 * $options = array(
27 * 'index' => 'elastic_index_name',
28 * 'type' => 'elastic_doc_type',
29 * );
30 * $handler = new ElasticSearchHandler($client, $options);
31 * $log = new Logger('application');
32 * $log->pushHandler($handler);
33 *
34 * @author Jelle Vink <jelle.vink@gmail.com>
35 */
36 class ElasticSearchHandler extends AbstractProcessingHandler
37 {
38 /**
39 * @var Client
40 */
41 protected $client;
42  
43 /**
44 * @var array Handler config options
45 */
46 protected $options = array();
47  
48 /**
49 * @param Client $client Elastica Client object
50 * @param array $options Handler configuration
51 * @param int $level The minimum logging level at which this handler will be triggered
52 * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
53 */
54 public function __construct(Client $client, array $options = array(), $level = Logger::DEBUG, $bubble = true)
55 {
56 parent::__construct($level, $bubble);
57 $this->client = $client;
58 $this->options = array_merge(
59 array(
60 'index' => 'monolog', // Elastic index name
61 'type' => 'record', // Elastic document type
62 'ignore_error' => false, // Suppress Elastica exceptions
63 ),
64 $options
65 );
66 }
67  
68 /**
69 * {@inheritDoc}
70 */
71 protected function write(array $record)
72 {
73 $this->bulkSend(array($record['formatted']));
74 }
75  
76 /**
77 * {@inheritdoc}
78 */
79 public function setFormatter(FormatterInterface $formatter)
80 {
81 if ($formatter instanceof ElasticaFormatter) {
82 return parent::setFormatter($formatter);
83 }
84 throw new \InvalidArgumentException('ElasticSearchHandler is only compatible with ElasticaFormatter');
85 }
86  
87 /**
88 * Getter options
89 * @return array
90 */
91 public function getOptions()
92 {
93 return $this->options;
94 }
95  
96 /**
97 * {@inheritDoc}
98 */
99 protected function getDefaultFormatter()
100 {
101 return new ElasticaFormatter($this->options['index'], $this->options['type']);
102 }
103  
104 /**
105 * {@inheritdoc}
106 */
107 public function handleBatch(array $records)
108 {
109 $documents = $this->getFormatter()->formatBatch($records);
110 $this->bulkSend($documents);
111 }
112  
113 /**
114 * Use Elasticsearch bulk API to send list of documents
115 * @param array $documents
116 * @throws \RuntimeException
117 */
118 protected function bulkSend(array $documents)
119 {
120 try {
121 $this->client->addDocuments($documents);
122 } catch (ExceptionInterface $e) {
123 if (!$this->options['ignore_error']) {
124 throw new \RuntimeException("Error sending messages to Elasticsearch", 0, $e);
125 }
126 }
127 }
128 }