1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-23 17:46:09 +02:00

Add elastic search formatter

This commit is contained in:
Avtandil Kikabidze
2018-04-07 03:10:46 +04:00
parent 57f3ca1791
commit 32563c49b7
2 changed files with 157 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
<?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;
/**
* 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($index, $type)
{
// ElasticSearch requires an ISO 8601 format date with optional millisecond precision.
parent::__construct('Y-m-d\TH:i:s.uP');
$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($record): array
{
$record['_index'] = $this->index;
$record['_type'] = $this->type;
return $record;
}
}