Handlers

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. # config/services.yaml
    2. services:
    3. Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler: ~
  • XML

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

    1. // config/services.php
    2. use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
    3. $container->register(ElasticsearchLogstashHandler::class);

Then reference it in the Monolog configuration:

  • YAML

    1. # config/packages/prod/monolog.yaml
    2. monolog:
    3. handlers:
    4. es:
    5. type: service
    6. id: Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler
  • XML

    1. <!-- config/packages/prod/monolog.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:monolog="http://symfony.com/schema/dic/monolog"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/monolog
    9. https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
    10. <monolog:config>
    11. <monolog:handler
    12. name="es"
    13. type="service"
    14. id="Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler"
    15. />
    16. </monolog:config>
    17. </container>
  • PHP

    1. // config/packages/prod/monolog.php
    2. use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
    3. $container->loadFromExtension('monolog', [
    4. 'handlers' => [
    5. 'es' => [
    6. 'type' => 'service',
    7. 'id' => ElasticsearchLogstashHandler::class,
    8. ],
    9. ],
    10. ]);

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