Monolog with Elasticsearch

Implementation of Elasticsearch with Monolog

Elasticsearch is a search engine based on Java LuceneElasticsearch 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

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.