Last Updated: July 25, 2019
·
2.977K
· stefanosala

How to configure Amqp handler (eg for RabbitMQ) with Monolog and Symfony2

You need php-amqp module to be enabled to use this handler.

First you have to create some services:

<parameters>
    <parameter key="acme.amqp.connection.parameters" type="collection">
        <parameter key="host">localhost</parameter>
        <parameter key="login">guest</parameter>
        <parameter key="password">guest</parameter>
        <parameter key="vhost">/</parameter>
    </parameter>
</parameters>

<services>
    <service id="acme.amqp.connection" class="AMQPConnection">
        <argument>%acme.amqp.connection.parameters%</argument>
        <call method="connect" />
    </service>

    <service id="acme.amqp.channel" class="AMQPChannel">
        <argument type="service" id="acme.amqp.connection" />
    </service>

    <service id="acme.amqp.exchange" class="AMQPExchange">
        <argument type="service" id="acme.amqp.channel" />
    </service>

    <service id="acme.monolog.amqp" class="Monolog\Handler\AmqpHandler">
        <argument type="service" id="acme.amqp.exchange" />
    </service>
</services>

than in your config_prod.yml add the last service as a handler for Monolog. This is my actual configuration:

monolog:
    handlers:
        main:
            type:               fingers_crossed
            action_level:       error
            handler:            grouped
        grouped:
            type:               group
            members:            [streamed, amqp]
        streamed:
            type:               stream
            path:               "%kernel.logs_dir%/%kernel.environment%.log"
            level:              debug
        login:
            type:               stream
            path:               "%kernel.logs_dir%/auth.log"
            level:              info
            channels:           security
        amqp:
            type:               service
            id:                 acmq.monolog.amqp
            level:              debug

Just edit it to your needs and environment.