Skip to content
Permalink
Browse files

Merge remote-tracking branch 'akalongman/feature/elasticsearch'

  • Loading branch information...
Seldaek committed Dec 8, 2018
2 parents 1d821e6 + be5c439 commit b978700c05f9405b289135be469870dc786fe9f9
@@ -6,13 +6,15 @@
* BC Break: Removed non-PSR-3 methods to add records, all the `add*` (e.g. `addWarning`) methods as well as `emerg`, `crit`, `err` and `warn`
* BC Break: The record timezone is now set per Logger instance and not statically anymore
* BC Break: There is no more default handler configured on empty Logger instances
* BC Break: ElasticSearchHandler renamed to ElasticaHandler
* BC Break: Various handler-specific breaks, see [UPGRADE.md] for details
* Added scalar type hints and return hints in all the places it was possible. Switched strict_types on for more reliability.
* Added DateTimeImmutable support, all record datetime are now immutable, and will toString/json serialize with the correct date format, including microseconds (unless disabled)
* Added timezone and microseconds to the default date format
* Added SendGridHandler to use the SendGrid API to send emails
* Added LogmaticHandler to use the Logmatic.io API to store log records
* Added SqsHandler to send log records to an AWS SQS queue
* Added ElasticsearchHandler to send records via the official ES library. Elastica users should now use ElasticaHandler instead of ElasticSearchHandler
* Added NoopHandler which is similar to the NullHandle but does not prevent the bubbling of log records to handlers further down the configuration, useful for temporarily disabling a handler in configuration files
* Added ProcessHandler to write log output to the STDIN of a given process
* Added HostnameProcessor that adds the machine's hostname to log records
@@ -57,3 +57,8 @@
#### HipChatHandler

- Removed HipChat API v1 support

#### ElasticSearchHandler

- As support for the official Elasticsearch library was added, the former ElasticSearchHandler has been
renamed to ElasticaHandler and the new one added as ElasticsearchHandler.
@@ -29,13 +29,15 @@
"jakub-onderka/php-parallel-lint": "^0.9",
"predis/predis": "^1.1",
"phpspec/prophecy": "^1.6.1",
"elasticsearch/elasticsearch": "^6.0",
"rollbar/rollbar": "^1.3"
},
"suggest": {
"graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
"sentry/sentry": "Allow sending log messages to a Sentry server",
"doctrine/couchdb": "Allow sending log messages to a CouchDB server",
"ruflin/elastica": "Allow sending log messages to an Elastic Search server",
"elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client",
"php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
"ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)",
@@ -81,7 +81,7 @@
[Mongo](http://pecl.php.net/package/mongo) extension connection.
- [_CouchDBHandler_](../src/Monolog/Handler/CouchDBHandler.php): Logs records to a CouchDB server.
- [_DoctrineCouchDBHandler_](../src/Monolog/Handler/DoctrineCouchDBHandler.php): Logs records to a CouchDB server via the Doctrine CouchDB ODM.
- [_ElasticSearchHandler_](../src/Monolog/Handler/ElasticSearchHandler.php): Logs records to an Elastic Search server.
- [_ElasticsearchHandler_](../src/Monolog/Handler/ElasticsearchHandler.php): Logs records to an Elasticsearch server.
- [_DynamoDbHandler_](../src/Monolog/Handler/DynamoDbHandler.php): Logs records to a DynamoDB table with the [AWS SDK](https://github.com/aws/aws-sdk-php).

### Wrappers / Special Handlers
@@ -65,6 +65,8 @@ public function getType(): string
/**
* Convert a log message into an Elastica Document
* @param array $record
* @return Document
*/
protected function getDocument(array $record): Document
{
@@ -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 @@
* '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 @@ public function setFormatter(FormatterInterface $formatter): HandlerInterface
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
@@ -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);
}
}
}
}
Oops, something went wrong.

0 comments on commit b978700

Please sign in to comment.
You can’t perform that action at this time.