| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php declare(strict_types=1);
- /*
- * This file is part of the Monolog package.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Monolog\Handler;
- use Monolog\Formatter\FormatterInterface;
- use Monolog\Formatter\ElasticaFormatter;
- use Monolog\Logger;
- use Elastica\Client;
- use Elastica\Exception\ExceptionInterface;
- /**
- * Elastic Search handler
- *
- * Usage example:
- *
- * $client = new \Elastica\Client();
- * $options = array(
- * 'index' => 'elastic_index_name',
- * 'type' => 'elastic_doc_type',
- * );
- * $handler = new ElasticaHandler($client, $options);
- * $log = new Logger('application');
- * $log->pushHandler($handler);
- *
- * @author Jelle Vink <jelle.vink@gmail.com>
- */
- class ElasticaHandler extends AbstractProcessingHandler
- {
- /**
- * @var Client
- */
- protected $client;
- /**
- * @var array Handler config options
- */
- protected $options = [];
- /**
- * @param Client $client Elastica Client object
- * @param array $options Handler configuration
- * @param int|string $level The minimum logging level at which this handler will be triggered
- * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
- */
- public function __construct(Client $client, array $options = [], $level = Logger::DEBUG, bool $bubble = true)
- {
- parent::__construct($level, $bubble);
- $this->client = $client;
- $this->options = array_merge(
- [
- 'index' => 'monolog', // Elastic index name
- 'type' => 'record', // Elastic document type
- 'ignore_error' => false, // Suppress Elastica exceptions
- ],
- $options
- );
- }
- /**
- * {@inheritDoc}
- */
- protected function write(array $record): void
- {
- $this->bulkSend([$record['formatted']]);
- }
- /**
- * {@inheritdoc}
- */
- public function setFormatter(FormatterInterface $formatter): HandlerInterface
- {
- if ($formatter instanceof ElasticaFormatter) {
- return parent::setFormatter($formatter);
- }
- throw new \InvalidArgumentException('ElasticaHandler is only compatible with ElasticaFormatter');
- }
- public function getOptions(): array
- {
- return $this->options;
- }
- /**
- * {@inheritDoc}
- */
- protected function getDefaultFormatter(): FormatterInterface
- {
- return new ElasticaFormatter($this->options['index'], $this->options['type']);
- }
- /**
- * {@inheritdoc}
- */
- public function handleBatch(array $records): void
- {
- $documents = $this->getFormatter()->formatBatch($records);
- $this->bulkSend($documents);
- }
- /**
- * Use Elasticsearch bulk API to send list of documents
- * @throws \RuntimeException
- */
- protected function bulkSend(array $documents): void
- {
- try {
- $this->client->addDocuments($documents);
- } catch (ExceptionInterface $e) {
- if (!$this->options['ignore_error']) {
- throw new \RuntimeException("Error sending messages to Elasticsearch", 0, $e);
- }
- }
- }
- }
|