laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

integration to Graylog (via Monolog ) #1272

Open lukylook opened 6 years ago

lukylook commented 6 years ago

@taylorotwell @JeffreyWay @themsaid

Hello please do "out of the box" integration to Graylog GELF via Monolog (like slack,syslog) ,

Graylog is free with GPL V3 License and nice alternative for personal use and small companies.

great for analysis ,reports and alerts . they have nice documents and very stable version.

Gray already have "native" integration with monolog since 2012

Laravel + Monolog + Graylog === :+1:

source: https://github.com/bzikarsky/gelf-php - gelf = biggest word to json :) https://github.com/Graylog2/graylog2-server http://docs.graylog.org/en/latest/pages/gelf.html https://www.graylog.org/overview

Thank You

crynobone commented 6 years ago

https://laravel.com/docs/5.6/logging#creating-channels-via-factories

I believe this should work.

'channels' => [
    'graylog' => [
        'driver' => 'custom',
        'via' => App\Logging\CreateGraylogLogger::class,
        'host' => 'graylog.yourcompany.com',
        'port' => 5141,
        'level' => Monolog\Logger::WARNING,
    ],
],
<?php

namespace App\Logging;

use Gelf\Publisher;
use Gelf\Transport\TcpTransport;
use Monolog\Handler\GelfHandler;
use Monolog\Logger;

class CreateGraylogLogger
{
    /**
     * Create a custom Monolog instance.
     *
     * @param  array  $config
     * @return \Monolog\Logger
     */
    public function __invoke(array $config)
    {
        $publisher = new Publisher(new TcpTransport($config['host'], $config['port']));

        return new Logger('graylog', [
            new GelfHandler($publisher, $config['level'])
        ]);
    }
}

You also need to include the following dependencies:

composer require "graylog2/gelf-php"
hakanersu commented 6 years ago

@crynobone methods works fine but if graylog connection failes app failes to. So i tried connect socket first

<?php

namespace App\Logging;

use Gelf\Publisher;
use Monolog\Logger;
use Gelf\Transport\TcpTransport;
use Monolog\Handler\GelfHandler;

class CreateGraylogLogger
{
    /**
     * Create a custom Monolog instance.
     *
     * @param  array  $config
     *
     * @return \Monolog\Logger| boolean
     */
    public function __invoke(array $config)
    {
        try {
           fsockopen($config['host'], $config['port'], $errno, $errstr, 2);
           $publisher = new Publisher(new TcpTransport($config['host'], $config['port']));

           return new Logger('graylog', [
               new GelfHandler($publisher, $config['level']),
           ]);
        } catch (\Exception $e) {
           return false;
        }
    }
}
lyndachiwetelu commented 5 years ago

You can now wrap your transport inIgnoreErrorTransportWrapper to make sure your application does not break if there is a failure in graylog connection.

See here: