ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ElasticSearchHandler.php
Go to the documentation of this file.
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 
16 use Monolog\Logger;
17 use Elastica\Client;
19 
37 {
41  protected $client;
42 
46  protected $options = array();
47 
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 
71  protected function write(array $record)
72  {
73  $this->bulkSend(array($record['formatted']));
74  }
75 
80  {
81  if ($formatter instanceof ElasticaFormatter) {
82  return parent::setFormatter($formatter);
83  }
84  throw new \InvalidArgumentException('ElasticSearchHandler is only compatible with ElasticaFormatter');
85  }
86 
91  public function getOptions()
92  {
93  return $this->options;
94  }
95 
99  protected function getDefaultFormatter()
100  {
101  return new ElasticaFormatter($this->options['index'], $this->options['type']);
102  }
103 
107  public function handleBatch(array $records)
108  {
109  $documents = $this->getFormatter()->formatBatch($records);
110  $this->bulkSend($documents);
111  }
112 
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 }
const DEBUG
Detailed debug information.
Definition: Logger.php:32
bulkSend(array $documents)
Use Elasticsearch bulk API to send list of documents.
__construct(Client $client, array $options=array(), $level=Logger::DEBUG, $bubble=true)
Base Handler class providing the Handler structure.
handleBatch(array $records)
{Handles a set of records at once.The records to handle (an array of record arrays)} ...
$records
Definition: simple_test.php:22
setFormatter(FormatterInterface $formatter)
{Sets the formatter.self}
getFormatter()
{Gets the formatter.FormatterInterface}
Create styles array
The data for the language used.
Format a log message into an Elastica Document.