Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages in full color showing how to combine Symfony with Docker, APIs, queues & async tasks, Webpack, Single-Page Applications, etc.

Buy printed version

Handlers

4.4 version
Maintained Unmaintained
5.0

Handlers

ElasticsearchLogstashHandler

New in version 4.4: The ElasticsearchLogstashHandler was introduced in Symfony 4.4.

This handler deals directly with the HTTP interface of Elasticsearch. This means it will slow down your application if Elasticsearch takes times to answer. Even if all HTTP calls are done asynchronously.

In a development environment, it’s fine to keep the default configuration: for each log, an HTTP request will be made to push the log to Elasticsearch.

In a production environment, it’s highly recommended to wrap this handler in a handler with buffering capabilities (like the FingersCrossedHandler or BufferHandler) in order to call Elasticsearch only once with a bulk push. For even better performance and fault tolerance, a proper ELK stack is recommended.

To use it, declare it as a service:

  • YAML
    1
    2
    3
    # config/services.yaml
    services:
        Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler: ~
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!-- config/services.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:monolog="http://symfony.com/schema/dic/monolog"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            https://symfony.com/schema/dic/services/services-1.0.xsd
            http://symfony.com/schema/dic/monolog
            https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
    
        <services>
            <service id="Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler"/>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    // config/services.php
    use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
    
    $container->register(ElasticsearchLogstashHandler::class);
    

Then reference it in the Monolog configuration:

  • YAML
    1
    2
    3
    4
    5
    6
    # config/packages/prod/monolog.yaml
    monolog:
        handlers:
            es:
                type: service
                id: Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <!-- config/packages/prod/monolog.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:monolog="http://symfony.com/schema/dic/monolog"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            https://symfony.com/schema/dic/services/services-1.0.xsd
            http://symfony.com/schema/dic/monolog
            https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
    
        <monolog:config>
            <monolog:handler
                name="es"
                type="service"
                id="Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler"
            />
        </monolog:config>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    // config/packages/prod/monolog.php
    use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
    
    $container->loadFromExtension('monolog', [
        'handlers' => [
            'es' => [
                'type' => 'service',
                'id' => ElasticsearchLogstashHandler::class,
            ],
        ],
    ]);
    

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.