ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
12namespace Monolog\Handler;
13
17use Elastica\Client;
18use Elastica\Exception\ExceptionInterface;
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 ),
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}
Format a log message into an Elastica Document.
getFormatter()
{Gets the formatter.FormatterInterface}
Base Handler class providing the Handler structure.
__construct(Client $client, array $options=array(), $level=Logger::DEBUG, $bubble=true)
write(array $record)
Writes the record down to the log of the implementing handler.void
setFormatter(FormatterInterface $formatter)
{{Sets the formatter.self}}
handleBatch(array $records)
{{Handles a set of records at once.}}
getDefaultFormatter()
Gets the default formatter.FormatterInterface
bulkSend(array $documents)
Use Elasticsearch bulk API to send list of documents.
Monolog log channel.
Definition: Logger.php:28
const DEBUG
Detailed debug information.
Definition: Logger.php:32
$records
Definition: simple_test.php:17