-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jesper Skovgaard Nielsen
committed
Jun 19, 2014
0 parents
commit 7a7e833
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Elasticsearch with logstash formatter | ||
|
||
This handler lets you put logs into Elasticsearch in the Logstash format, | ||
which makes visualization with Kibana very easy. | ||
|
||
## Recommended setup | ||
|
||
```php | ||
$client = new Elasticsearch\Client(['hosts' => ['http://example.com:9200']]); | ||
$formatter = new Monolog\Formatter\LogstashFormatter('application', null, null, '', 1); | ||
$handler = new Monolog\ElasticLogstashHandler($client, ['type' => 'invoicing-logs']); | ||
$handler->setFormatter($formatter); | ||
|
||
|
||
$log = new Monolog\Logger(''); | ||
$log->pushHandler($handler); | ||
$log->warn('new sale', ['user_id' => 42, 'product_id' => 7537]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "nulpunkt/monolog-elasticsearch-logstashformat", | ||
"type": "library", | ||
"description": "Use the logstash formatter with elasticsearch", | ||
"keywords": ["elasticsearch", "monolog", "logstash", "kibana"], | ||
"homepage": "http://github.com/nulpunkt/monolog-elasticsearch-logstashformat", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jesper Skovgård Nielsen", | ||
"email": "[email protected]", | ||
"homepage": "http://runlevel0.dk", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0", | ||
"elasticsearch/elasticsearch": "~1.0", | ||
"monolog/monolog": "1.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Monolog\\": "src/Monolog" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Monolog; | ||
|
||
class ElasticLogstashHandler extends \Monolog\Handler\AbstractProcessingHandler | ||
{ | ||
/** | ||
* @param Client $client ElasticSearch Client object | ||
* @param array $options Handler configuration | ||
* @param integer $level The minimum logging level at which this handler will be triggered | ||
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not | ||
*/ | ||
public function __construct($client, array $options = array(), $level = \Monolog\Logger::DEBUG, $bubble = true) | ||
{ | ||
parent::__construct($level, $bubble); | ||
$this->client = $client; | ||
$this->options = array_merge( | ||
array( | ||
'index' => 'logstash-'.date('Y.m.d'), // Elastic index name | ||
'type' => 'logs', // Elastic document type | ||
'ignore_error' => false, // Suppress exceptions | ||
), | ||
$options | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function write(array $record) | ||
{ | ||
try { | ||
$this->client->index( | ||
[ | ||
'index' => $this->options['index'], | ||
'type' => $this->options['type'], | ||
'timeout' => 50, | ||
'body' => json_decode($record['formatted'], true) | ||
] | ||
); | ||
} catch (\Exception $e) { | ||
// Well that didn't pan out... | ||
if (!$this->options['ignore_error']) { | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setFormatter(\Monolog\Formatter\FormatterInterface $formatter) | ||
{ | ||
return parent::setFormatter($formatter); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function getDefaultFormatter() | ||
{ | ||
return new \Monolog\Formatter\LogstashFormatter(''); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleBatch(array $records) | ||
{ | ||
foreach ($records as $record) { | ||
$this->write($records); | ||
} | ||
} | ||
} |