├── README.md ├── composer.json └── src └── Monolog └── ElasticLogstashHandler.php /README.md: -------------------------------------------------------------------------------- 1 | # Elasticsearch with logstash formatter 2 | 3 | This handler lets you put logs into Elasticsearch in the Logstash format, 4 | which makes visualization with Kibana very easy. 5 | 6 | ## Recommended setup 7 | 8 | ```php 9 | $client = new Elasticsearch\Client(['hosts' => ['http://example.com:9200']]); 10 | $formatter = new Monolog\Formatter\LogstashFormatter('application', null, null, '', 1); 11 | $handler = new Monolog\ElasticLogstashHandler($client, ['type' => 'invoicing-logs']); 12 | $handler->setFormatter($formatter); 13 | 14 | 15 | $log = new Monolog\Logger('invoicing'); 16 | $log->pushHandler($handler); 17 | $log->warn('new sale', ['user_id' => 42, 'product_id' => 7537]); 18 | ``` 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nulpunkt/monolog-elasticsearch-logstashformat", 3 | "type": "library", 4 | "description": "Use the logstash formatter with elasticsearch", 5 | "keywords": ["elasticsearch", "monolog", "logstash", "kibana"], 6 | "homepage": "http://github.com/nulpunkt/monolog-elasticsearch-logstashformat", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Jesper Skovgård Nielsen", 11 | "email": "nulpunkt@gmail.com", 12 | "homepage": "http://runlevel0.dk", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.3.0", 18 | "elasticsearch/elasticsearch": "~1.0", 19 | "monolog/monolog": "1.*" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Monolog\\": "src/Monolog" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Monolog/ElasticLogstashHandler.php: -------------------------------------------------------------------------------- 1 | client = $client; 17 | $this->options = array_merge( 18 | array( 19 | 'index' => 'logstash-'.date('Y.m.d'), // Elastic index name 20 | 'type' => 'logs', // Elastic document type 21 | 'ignore_error' => false, // Suppress exceptions 22 | ), 23 | $options 24 | ); 25 | } 26 | 27 | /** 28 | * {@inheritDoc} 29 | */ 30 | protected function write(array $record) 31 | { 32 | try { 33 | $this->client->index( 34 | [ 35 | 'index' => $this->options['index'], 36 | 'type' => $this->options['type'], 37 | 'timeout' => '50ms', 38 | 'body' => json_decode($record['formatted'], true) 39 | ] 40 | ); 41 | } catch (\Exception $e) { 42 | // Well that didn't pan out... 43 | if (!$this->options['ignore_error']) { 44 | throw $e; 45 | } 46 | } 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function setFormatter(\Monolog\Formatter\FormatterInterface $formatter) 53 | { 54 | return parent::setFormatter($formatter); 55 | } 56 | 57 | /** 58 | * {@inheritDoc} 59 | */ 60 | protected function getDefaultFormatter() 61 | { 62 | return new \Monolog\Formatter\LogstashFormatter(''); 63 | } 64 | 65 | /** 66 | * {@inheritdoc} 67 | */ 68 | public function handleBatch(array $records) 69 | { 70 | foreach ($records as $record) { 71 | $this->write($records); 72 | } 73 | } 74 | } 75 | --------------------------------------------------------------------------------