2017 © Pedro Peláez
 

library elasticlog

Library for sending application data to elasticsearch.

image

jakubkratina/elasticlog

Library for sending application data to elasticsearch.

  • Wednesday, June 20, 2018
  • by jakub.kratina
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Installation

Require package via composer, (*1)

composer require jakubkratina/elasticlog

Configuration

ELASTIC_ENABLED=true
ELASTIC_INDEX=index-name
ELASTIC_HOST_PORT=elasticsearch.example.com:9200

Register logger

Laravel

$app->bind(
    \JK\Elasticlog\Contracts\Elasticsearch\Client::class,
    function () {
        if (env('ELASTIC_ENABLED', false) === true) {
            return new \JK\Elasticlog\Elasticsearch\Client(
                \Elasticsearch\ClientBuilder::create()->setHosts([env('ELASTIC_HOST_PORT')])->build(),
                env('ELASTIC_INDEX')
            );
        }

        return new \JK\Elasticlog\Elasticsearch\NullClient();
    }
);

Symfony

You can register client via factory class:, (*2)

Register client into DI
    app.service.elasticlog:
        factory: ['AppBundle\Logger\ClientFactory', create]
Create factory class
use JK\Elasticlog\Contracts\Elasticsearch\Client;
use JK\Elasticlog\Elasticsearch\Client as ElasticClient;
use JK\Elasticlog\Elasticsearch\NullClient;
use Elasticsearch\ClientBuilder;

final class ClientFactory
{
    /**
     * @return Client
     */
    public static function create(): Client
    {
        return self::isElasticsearchEnabled()
            ? self::createElasticClient()
            : self::createNullClient();
    }

    /**
     * @return Client
     */
    private static function createNullClient(): Client
    {
        return new NullClient();
    }

    /**
     * @return Client
     */
    private static function createElasticClient(): Client
    {
        return new ElasticClient(
            ClientBuilder::create()->setHosts([getenv('ELASTIC_HOST_PORT')])->build(), getenv('ELASTIC_INDEX')
        );
    }

    /**
     * @return bool
     */
    private static function isElasticsearchEnabled(): bool
    {
        return getenv('ELASTIC_ENABLED') === 'true';
    }
}

Usage

Create a message

Create a new class extending from JK\Elasticlog\Log\Message and implement the toArray method., (*3)

class MyCustomMessage extends \JK\Elasticlog\Log\Message
{
    public function toArray(): array
    {
        return [
            'foo' => 'bar'
        ];
    }
}

You are free to pass parameters via constructor:, (*4)

class MyCustomMessage extends \JK\Elasticlog\Log\Message
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function toArray(): array
    {
        return [
            'foo' => 'bar',
            'name' => ucfirst($this->name)
        ];
    }
}
$message = new MyCustomMessage();

// ... your code

$logger->log($message); 

The duration between creating and logging a message is measured out of the box as a duration property., (*5)

Available methods

toArray, add, append, merge, (*6)

$message = (new Messages)->fooBarMessage();
$message->toArray(); // ['foo' => 'bar']

$message->add('a', (new Messages)->fooBarMessage());
$message->add('b', (new Messages)->barBazMessage());

$message->append((new Messages)->fooBarMessage());
$message->append((new Messages)->barBazMessage());

$message->merge([
    'x' => 'y',
]);

$this->assertEquals([
    'foo' => 'bar',
    'a'   => [
        'foo' => 'bar',
    ],
    'b'   => [
        'bar' => 'baz',
    ],
    'bar' => 'baz',
    'x'   => 'y',
], $message->build());

The Versions

20/06 2018

dev-master

9999999-dev

Library for sending application data to elasticsearch.

  Sources   Download

The Requires

 

The Development Requires

by Jakub Kratina

25/03 2018

1.2

1.2.0.0

Library for sending application data to elasticsearch.

  Sources   Download

The Requires

 

The Development Requires

by Jakub Kratina

18/03 2018

1.1

1.1.0.0

Library for sending application data to elasticsearch.

  Sources   Download

The Requires

 

The Development Requires

by Jakub Kratina

18/03 2018

1.0

1.0.0.0

Library for sending application data to elasticsearch.

  Sources   Download

The Requires

 

The Development Requires

by Jakub Kratina