ElasticaHandler.php 3.7 KB

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