mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-21 16:46:11 +02:00
Merge remote-tracking branch 'akalongman/feature/elasticsearch'
This commit is contained in:
@@ -65,6 +65,8 @@ class ElasticaFormatter extends NormalizerFormatter
|
||||
|
||||
/**
|
||||
* Convert a log message into an Elastica Document
|
||||
* @param array $record
|
||||
* @return Document
|
||||
*/
|
||||
protected function getDocument(array $record): Document
|
||||
{
|
||||
|
89
src/Monolog/Formatter/ElasticsearchFormatter.php
Normal file
89
src/Monolog/Formatter/ElasticsearchFormatter.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?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\Formatter;
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* Format a log message into an Elasticsearch record
|
||||
*
|
||||
* @author Avtandil Kikabidze <akalongman@gmail.com>
|
||||
*/
|
||||
class ElasticsearchFormatter extends NormalizerFormatter
|
||||
{
|
||||
/**
|
||||
* @var string Elasticsearch index name
|
||||
*/
|
||||
protected $index;
|
||||
|
||||
/**
|
||||
* @var string Elasticsearch record type
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @param string $index Elasticsearch index name
|
||||
* @param string $type Elasticsearch record type
|
||||
*/
|
||||
public function __construct(string $index, string $type)
|
||||
{
|
||||
// Elasticsearch requires an ISO 8601 format date with optional millisecond precision.
|
||||
parent::__construct(DateTime::ISO8601);
|
||||
|
||||
$this->index = $index;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function format(array $record)
|
||||
{
|
||||
$record = parent::format($record);
|
||||
|
||||
return $this->getDocument($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter index
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIndex(): string
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a log message into an Elasticsearch record
|
||||
*
|
||||
* @param array $record Log message
|
||||
* @return array
|
||||
*/
|
||||
protected function getDocument(array $record): array
|
||||
{
|
||||
$record['_index'] = $this->index;
|
||||
$record['_type'] = $this->type;
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
@@ -27,13 +27,13 @@ use Elastica\Exception\ExceptionInterface;
|
||||
* 'index' => 'elastic_index_name',
|
||||
* 'type' => 'elastic_doc_type',
|
||||
* );
|
||||
* $handler = new ElasticSearchHandler($client, $options);
|
||||
* $handler = new ElasticaHandler($client, $options);
|
||||
* $log = new Logger('application');
|
||||
* $log->pushHandler($handler);
|
||||
*
|
||||
* @author Jelle Vink <jelle.vink@gmail.com>
|
||||
*/
|
||||
class ElasticSearchHandler extends AbstractProcessingHandler
|
||||
class ElasticaHandler extends AbstractProcessingHandler
|
||||
{
|
||||
/**
|
||||
* @var Client
|
||||
@@ -81,8 +81,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
|
||||
if ($formatter instanceof ElasticaFormatter) {
|
||||
return parent::setFormatter($formatter);
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException('ElasticSearchHandler is only compatible with ElasticaFormatter');
|
||||
throw new \InvalidArgumentException('ElasticaHandler is only compatible with ElasticaFormatter');
|
||||
}
|
||||
|
||||
public function getOptions(): array
|
158
src/Monolog/Handler/ElasticsearchHandler.php
Normal file
158
src/Monolog/Handler/ElasticsearchHandler.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?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 Elasticsearch\Client;
|
||||
use Elasticsearch\Common\Exceptions\RuntimeException as ElasticsearchRuntimeException;
|
||||
use InvalidArgumentException;
|
||||
use Monolog\Formatter\ElasticsearchFormatter;
|
||||
use Monolog\Formatter\FormatterInterface;
|
||||
use Monolog\Logger;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Elasticsearch handler
|
||||
*
|
||||
* @link https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
|
||||
*
|
||||
* Simple usage example:
|
||||
*
|
||||
* $client = \Elasticsearch\ClientBuilder::create()
|
||||
* ->setHosts($hosts)
|
||||
* ->build();
|
||||
*
|
||||
* $options = array(
|
||||
* 'index' => 'elastic_index_name',
|
||||
* 'type' => 'elastic_doc_type',
|
||||
* );
|
||||
* $handler = new ElasticsearchHandler($client, $options);
|
||||
* $log = new Logger('application');
|
||||
* $log->pushHandler($handler);
|
||||
*
|
||||
* @author Avtandil Kikabidze <akalongman@gmail.com>
|
||||
*/
|
||||
class ElasticsearchHandler extends AbstractProcessingHandler
|
||||
{
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @var array Handler config options
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* @param Client $client Elasticsearch Client object
|
||||
* @param array $options Handler configuration
|
||||
* @param string|int $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' => '_doc', // Elastic document type
|
||||
'ignore_error' => false, // Suppress Elasticsearch exceptions
|
||||
],
|
||||
$options
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function write(array $record): void
|
||||
{
|
||||
$this->bulkSend([$record['formatted']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setFormatter(FormatterInterface $formatter): HandlerInterface
|
||||
{
|
||||
if ($formatter instanceof ElasticsearchFormatter) {
|
||||
return parent::setFormatter($formatter);
|
||||
}
|
||||
throw new InvalidArgumentException('ElasticsearchHandler is only compatible with ElasticsearchFormatter');
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getDefaultFormatter(): FormatterInterface
|
||||
{
|
||||
return new ElasticsearchFormatter($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
|
||||
*
|
||||
* @param array $records
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected function bulkSend(array $records): void
|
||||
{
|
||||
try {
|
||||
$params = [
|
||||
'body' => [],
|
||||
];
|
||||
|
||||
foreach ($records as $record) {
|
||||
$params['body'][] = [
|
||||
'index' => [
|
||||
'_index' => $record['_index'],
|
||||
'_type' => $record['_type'],
|
||||
],
|
||||
];
|
||||
unset($record['_index'], $record['_type']);
|
||||
|
||||
$params['body'][] = $record;
|
||||
}
|
||||
|
||||
$responses = $this->client->bulk($params);
|
||||
|
||||
if ($responses['errors'] === true) {
|
||||
throw new ElasticsearchRuntimeException('Elasticsearch returned error for one of the records');
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
if (! $this->options['ignore_error']) {
|
||||
throw new RuntimeException('Error sending messages to Elasticsearch', 0, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user