Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Logging
  4. Handlers
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia
  • ElasticsearchLogstashHandler

Handlers

Edit this page

Handlers

ElasticsearchLogstashHandler

This handler deals directly with the HTTP interface of Elasticsearch. This means it will slow down your application if Elasticsearch takes time 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
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
# config/services.yaml
services:
    Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler: ~

    # optionally, configure the handler using the constructor arguments (shown values are default)
    Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler: ~
        arguments:
            $endpoint: "http://127.0.0.1:9200"
            $index: "monolog"
            $client: null
            $level: !php/const Monolog\Logger::DEBUG
            $bubble: true
            $elasticsearchVersion: '1.0.0'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- 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"/>

        <!-- optionally, configure the handler using the constructor arguments (shown values are default) -->
        <service id="Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler">
            <argument key="endpoint">http://127.0.0.1:9200</argument>
            <argument key="index">monolog</argument>
            <argument key="client"/>
            <argument key="level" type="constant">Monolog\Logger::DEBUG</argument>
            <argument key="bubble">true</argument>
            <argument key="elasticsearchVersion">1.0.0</argument>
        </service>
    </services>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// config/services.php
use Monolog\Logger;
use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;

$container->register(ElasticsearchLogstashHandler::class);

// optionally, configure the handler using the constructor arguments (shown values are default)
$container->register(ElasticsearchLogstashHandler::class)
    ->setArguments(
        '$endpoint' => "http://127.0.0.1:9200",
        '$index' => "monolog",
        '$client' => null,
        '$level' => Logger::DEBUG,
        '$bubble' => true,
        '$elasticsearchVersion' => '1.0.0',
    )
;

Then reference it in the Monolog configuration:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
# config/packages/prod/monolog.yaml
monolog:
    handlers:
        es:
            type: service
            id: Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler
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>
1
2
3
4
5
6
7
8
9
10
// config/packages/prod/monolog.php
use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
    $monolog->handler('es')
        ->type('service')
        ->id(ElasticsearchLogstashHandler::class)
    ;
};
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:

Symfony 6.2 is backed by

Symfony 6.2 is backed by

Symfony Code Performance Profiling

Symfony Code Performance Profiling

Peruse our complete Symfony & PHP solutions catalog for your web development needs.

Peruse our complete Symfony & PHP solutions catalog for your web development needs.

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Mark, a Symfony contributor

Thanks Mark (@markchicobaby) for being a Symfony contributor

4 commits • 26 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia