forked from symfony/symfony
/
ElasticsearchLogstashHandler.php
156 lines (134 loc) · 4.92 KB
/
ElasticsearchLogstashHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LogstashFormatter;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\FormattableHandlerTrait;
use Monolog\Handler\ProcessableHandlerTrait;
use Monolog\Logger;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Push logs directly to Elasticsearch and format them according to Logstash specification.
*
* This handler dials directly with the HTTP interface of Elasticsearch. This
* means it will slow down your application if Elasticsearch takes times to
* answer. Even if all HTTP calls are done asynchronously.
*
* In a development environment, it's fine to keep the default configuration:
* for each log, an HTTP request will be made to push the log to Elasticsearch.
*
* In a production environment, it's highly recommended to wrap this handler
* in a handler with buffering capabilities (like the FingersCrossedHandler, or
* BufferHandler) in order to call Elasticsearch only once with a bulk push. For
* even better performance and fault tolerance, a proper ELK (https://www.elastic.co/what-is/elk-stack)
* stack is recommended.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class ElasticsearchLogstashHandler extends AbstractHandler
{
use ProcessableHandlerTrait;
use FormattableHandlerTrait;
private $endpoint;
private $index;
private $client;
private $responses;
/**
* @param string|int $level The minimum logging level at which this handler will be triggered
*/
public function __construct(string $endpoint = 'http://127.0.0.1:9200', string $index = 'monolog', HttpClientInterface $client = null, $level = Logger::DEBUG, bool $bubble = true)
{
if (!interface_exists(HttpClientInterface::class)) {
throw new \LogicException(sprintf('The "%s" handler needs an HTTP client. Try running "composer require symfony/http-client".', __CLASS__));
}
parent::__construct($level, $bubble);
$this->endpoint = $endpoint;
$this->index = $index;
$this->client = $client ?: HttpClient::create(['timeout' => 1]);
$this->responses = new \SplObjectStorage();
}
public function handle(array $record): bool
{
if (!$this->isHandling($record)) {
return false;
}
$this->sendToElasticsearch([$record]);
return !$this->bubble;
}
public function handleBatch(array $records): void
{
$records = array_filter($records, [$this, 'isHandling']);
if ($records) {
$this->sendToElasticsearch($records);
}
}
protected function getDefaultFormatter(): FormatterInterface
{
// Monolog 1.X
if (\defined(LogstashFormatter::class.'::V1')) {
return new LogstashFormatter('application', null, null, 'ctxt_', LogstashFormatter::V1);
}
// Monolog 2.X
return new LogstashFormatter('application');
}
private function sendToElasticsearch(array $records)
{
$formatter = $this->getFormatter();
$body = '';
foreach ($records as $record) {
foreach ($this->processors as $processor) {
$record = $processor($record);
}
$body .= json_encode([
'index' => [
'_index' => $this->index,
'_type' => '_doc',
],
]);
$body .= "\n";
$body .= $formatter->format($record);
$body .= "\n";
}
$response = $this->client->request('POST', $this->endpoint.'/_bulk', [
'body' => $body,
'headers' => [
'Content-Type' => 'application/json',
],
]);
$this->responses->attach($response);
$this->wait(false);
}
public function __destruct()
{
$this->wait(true);
}
private function wait(bool $blocking)
{
foreach ($this->client->stream($this->responses, $blocking ? null : 0.0) as $response => $chunk) {
try {
if ($chunk->isTimeout() && !$blocking) {
continue;
}
if (!$chunk->isFirst() && !$chunk->isLast()) {
continue;
}
if ($chunk->isLast()) {
$this->responses->detach($response);
}
} catch (ExceptionInterface $e) {
$this->responses->detach($response);
error_log(sprintf("Could not push logs to Elasticsearch:\n%s", (string) $e));
}
}
}
}