Elasticsearch is a search engine based on Java Lucene. Elasticsearch provides a distributed, full-text search engine. It works with an HTTP protocol and schema-free JSON documents. And the monolog is the PHP library to manage application logs. It’s most popular library for PHP. Monolog has a lot of search handler and Elasticsearch is one of them. So that in this article I am showing implementation Elasticsearch with monolog.
Code Snippet:
File: composer.json
{
"require": {
"php": ">=5.5.0",
"ruflin/elastica": "dev-master",
"monolog/monolog": "^1.22"
}
}
File: index.php
<?php
require 'vendor/autoload.php';
use Monolog\Handler\ElasticSearchHandler;
use Monolog\Logger;
use Elastica\Client;
$client = new Client();
$options = array(
'index' => 'elastic_search_logs',
'type' => 'elastic_doc_type',
);
$handler = new ElasticSearchHandler($client, $options);
$log = new Logger('application_logs');
$log->pushHandler($handler);
$log->error('Write your error message');
$log->info('Write your info message');
$log->warn('Write your info message');
From this snippet, you can understand the Implementation of Elasticsearch with Monolog