Create a gist now

Instantly share code, notes, and snippets.

@Spea /deps
Created Apr 24, 2012

What would you like to do?
Gelf handler implementation for Symfony 2.0
[monolog]
git=http://github.com/Seldaek/monolog.git
version=1.1.0
[gelf-php]
git=http://github.com/mlehner/gelf-php.git
version=v1.0
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="monolog.handler.gelf.class">Monolog\Handler\GelfHandler</parameter>
<parameter key="monolog.handler.gelf.debug_level">200</parameter> <!-- since we can not use strings here, we have to use the integer value of the debug levels, 200 is deubbing level INFO -->
<parameter key="gelf.publisher.class">Gelf\MessagePublisher</parameter>
<parameter key="graylog.hostname">localhost</parameter>
<parameter key="graylog.port">12201</parameter>
</parameters>
<services>
<service id="gelf.publisher" class="%gelf.publisher.class%">
<argument>%graylog.hostname%</argument>
<argument>%graylog.port%</argument>
</service>
<service id="monolog.gelf_handler" class="%monolog.handler.gelf.class%">
<argument type="service" id="gelf.publisher" />
<argument>%monolog.handler.gelf.debug_level%</argument>
</service>
</services>
</container>

Tying all together.

The gelf.xml should be placed in the Directory Resources/config in one of your bundles (for example Acme\Bundle). Then adjust the extension file of this bundle (in our case it would be Acme\Bundle\DependencyInjection\AcmeExtensions).

<?php

namespace Acme\Bundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class AcmeExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        // ...
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('gelf.xml');
        // ...
    }
}

Adjust the parameters to your needs (parameters.yml/parameters.ini):

parameters:
    graylog.hostname: graylog.example.com
    graylog.port: 29833 # only adjust it when you don't use the default port
    
    # DEBUG
    # monolog.handler.gelf.debug_level: 100
    # INFO (default)
    monolog.handler.gelf.debug_level: 200
    # WARNING
    # monolog.handler.gelf.debug_level: 300
    # ERROR
    # monolog.handler.gelf.debug_level: 400
    # CRITICAL
    # monolog.handler.gelf.debug_level: 500
    # ALERT
    # monolog.handler.gelf.debug_level: 550

Finally you can add the gelf handler in your config.yml:

# ...
monolog:
    handlers:
        gelf:
          type: service
          id: monolog.gelf_handler
# ...

Hello thanks for this.
How can I do the same config in drupal 8 only with yaml file.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment