laravelhandlermonologloggly

How to send Log event from Laravel to Loggly?


I want to send Monolog logs from my Laravel 5.1 application to Loggly.com online log management service. From all possible environment, including local development.


Solution

  • I have found some outdated libs and complicated ways to do this. So I ended up with very simple solution. Actually, Laravel Monolog Handler already have Loggly Handler out of the box.

    Add config info to config/services.php:

    'loggly' => array(
        'key'   => 'ENTER_YOUR_LOGGLY_TOKEN_HERE',
        'tag'   => 'ProjectName_' .strtolower(env('APP_ENV')),
    ),
    

    Than add Monolog handler in bootstrap/app.php, before $app is returned:

    /*
    |--------------------------------------------------------------------------
    | Setup Loggly Handler
    |--------------------------------------------------------------------------
    */
    $app->configureMonologUsing(function($monolog) {
        $handler = new      \Monolog\Handler\LogglyHandler(config('services.loggly.key'),\Monolog\Logger::DEBUG);
        $handler->setTag(config('services.loggly.tag'));
    
        $monolog->pushHandler($handler);
    });
    

    Voila! You are getting your Monolog Logs in Loggly dashboard.


    UPDATE: (thanks @thitami)

    Based on laravel.com/docs/5.6/upgrade

    The configureMonologUsing Method If you were using the configureMonologUsing method to customize the Monolog instance for your application, you should now create a custom Log channel. For more information on how to create custom channels, check out the full logging documentation.