Skip to content

Commit

Permalink
Added ElasticLogstashHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesper Skovgaard Nielsen committed Jun 19, 2014
0 parents commit 7a7e833
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
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]);
```
26 changes: 26 additions & 0 deletions composer.json
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"
}
}
}
74 changes: 74 additions & 0 deletions src/Monolog/ElasticLogstashHandler.php
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);
}
}
}

0 comments on commit 7a7e833

Please sign in to comment.