Skip to content

Handlers

Warning: You are browsing the documentation for Symfony 7.0, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (the current stable version).

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.

To use it, declare it as a service:

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/enum Monolog\Level::Debug
            $bubble: true
            $elasticsearchVersion: '1.0.0'

Then reference it in the Monolog configuration.

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:

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

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.

1
2
3
4
5
6
7
8
9
10
# config/packages/prod/monolog.yaml
monolog:
    handlers:
        main:
            type: fingers_crossed
            handler: es

        es:
            type: service
            id: Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version