media-cloud-sync
/
includes
/
sdk
/
google
/
monolog
/
monolog
/
src
/
Monolog
/
Handler
/
ElasticaHandler.php
ElasticaHandler.php in Media Cloud Sync 1.2.5, at includes/sdk/google/monolog/monolog/src/Monolog/Handler/ElasticaHandler.php
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | /* |
| 5 | * This file is part of the Monolog package. |
| 6 | * |
| 7 | * (c) Jordi Boggiano <[email protected]> |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | namespace Dudlewebs\WPMCS\Monolog\Handler; |
| 13 | |
| 14 | use Dudlewebs\WPMCS\Elastica\Document; |
| 15 | use Dudlewebs\WPMCS\Monolog\Formatter\FormatterInterface; |
| 16 | use Dudlewebs\WPMCS\Monolog\Formatter\ElasticaFormatter; |
| 17 | use Dudlewebs\WPMCS\Monolog\Logger; |
| 18 | use Dudlewebs\WPMCS\Elastica\Client; |
| 19 | use Dudlewebs\WPMCS\Elastica\Exception\ExceptionInterface; |
| 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', Types have been removed in Elastica 7 |
| 29 | * ); |
| 30 | * $handler = new ElasticaHandler($client, $options); |
| 31 | * $log = new Logger('application'); |
| 32 | * $log->pushHandler($handler); |
| 33 | * |
| 34 | * @author Jelle Vink <[email protected]> |
| 35 | */ |
| 36 | class ElasticaHandler extends AbstractProcessingHandler |
| 37 | { |
| 38 | /** |
| 39 | * @var Client |
| 40 | */ |
| 41 | protected $client; |
| 42 | /** |
| 43 | * @var mixed[] Handler config options |
| 44 | */ |
| 45 | protected $options = []; |
| 46 | /** |
| 47 | * @param Client $client Elastica Client object |
| 48 | * @param mixed[] $options Handler configuration |
| 49 | */ |
| 50 | public function __construct(Client $client, array $options = [], $level = Logger::DEBUG, bool $bubble = \true) |
| 51 | { |
| 52 | parent::__construct($level, $bubble); |
| 53 | $this->client = $client; |
| 54 | $this->options = array_merge([ |
| 55 | 'index' => 'monolog', |
| 56 | // Elastic index name |
| 57 | 'type' => 'record', |
| 58 | // Elastic document type |
| 59 | 'ignore_error' => \false, |
| 60 | ], $options); |
| 61 | } |
| 62 | /** |
| 63 | * {@inheritDoc} |
| 64 | */ |
| 65 | protected function write(array $record): void |
| 66 | { |
| 67 | $this->bulkSend([$record['formatted']]); |
| 68 | } |
| 69 | /** |
| 70 | * {@inheritDoc} |
| 71 | */ |
| 72 | public function setFormatter(FormatterInterface $formatter): HandlerInterface |
| 73 | { |
| 74 | if ($formatter instanceof ElasticaFormatter) { |
| 75 | return parent::setFormatter($formatter); |
| 76 | } |
| 77 | throw new \InvalidArgumentException('ElasticaHandler is only compatible with ElasticaFormatter'); |
| 78 | } |
| 79 | /** |
| 80 | * @return mixed[] |
| 81 | */ |
| 82 | public function getOptions(): array |
| 83 | { |
| 84 | return $this->options; |
| 85 | } |
| 86 | /** |
| 87 | * {@inheritDoc} |
| 88 | */ |
| 89 | protected function getDefaultFormatter(): FormatterInterface |
| 90 | { |
| 91 | return new ElasticaFormatter($this->options['index'], $this->options['type']); |
| 92 | } |
| 93 | /** |
| 94 | * {@inheritDoc} |
| 95 | */ |
| 96 | public function handleBatch(array $records): void |
| 97 | { |
| 98 | $documents = $this->getFormatter()->formatBatch($records); |
| 99 | $this->bulkSend($documents); |
| 100 | } |
| 101 | /** |
| 102 | * Use Elasticsearch bulk API to send list of documents |
| 103 | * |
| 104 | * @param Document[] $documents |
| 105 | * |
| 106 | * @throws \RuntimeException |
| 107 | */ |
| 108 | protected function bulkSend(array $documents): void |
| 109 | { |
| 110 | try { |
| 111 | $this->client->addDocuments($documents); |
| 112 | } catch (ExceptionInterface $e) { |
| 113 | if (!$this->options['ignore_error']) { |
| 114 | throw new \RuntimeException("Error sending messages to Elasticsearch", 0, $e); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 |