Mladen Janjetovic
Mladen Janjetovic

Reputation: 14714

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.

Upvotes: 5

Views: 3541

Answers (5)

Muhaimin Taib
Muhaimin Taib

Reputation: 336

Got mine working with little configuration with Laravel 8.

Just use the built-in monolog handler for Loggly.

Edit your app/config/logging.php

use Monolog\Handler\LogglyHandler;

'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single', 'loggly'],
            'ignore_exceptions' => false,
        ],

        'loggly' => [
            'driver' => 'monolog',
            'level' => env('LOG_LEVEL', 'debug'),
            'handler' => LogglyHandler::class,
            'with' => [
                'token' => env('LOGGLY_TOKEN'),
            ],
        ],
   ]

For more advanced logging (for my case I need to set the tag as it was missing in the built-in handler's constructor.

Copy the built-in handler where you can find it within vendor folder (e.g: vendor/monolog/monolog/src/Monolog/Handler/LogglyHandler.php) into your app folder of choice (e.g: app/Logging/CustomLogglyHandler.php).

Modify the constructor to set the tags, and you need to change some of the imports as we're on different namespaces.

// app/Logging/CustomLogglyHandler.php

namespace App\Logging;

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\MissingExtensionException;
use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LogglyFormatter;
use function array_key_exists;
use CurlHandle;
use Monolog\Handler\Curl\Util as CurlUtil;

  public function __construct(string $token, array|string $tag = [], $level = Logger::DEBUG, bool $bubble = true)
  {
    if (!extension_loaded('curl')) {
      throw new MissingExtensionException('The curl extension is needed to use the LogglyHandler');
    }

    $this->token = $token;

    if (is_array($tag)) {
      $this->tag = $tag;
    } else {
      $this->tag = [$tag];
    }

    parent::__construct($level, $bubble);
  }


// config/logging.php


'loggly' => [
        'driver' => 'monolog',
        'level' => env('LOG_LEVEL', 'debug'),
        'handler' => CustomLogglyHandler::class,
        'with' => [
             'token' => env('LOGGLY_TOKEN'),
             'tag'   => strtolower(env('APP_NAME', 'Laravel')) . '_' . strtolower(env('APP_ENV', 'production'))
            ],
        ],

Upvotes: 3

Mladen Janjetovic
Mladen Janjetovic

Reputation: 14714

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.

Upvotes: 10

Raph
Raph

Reputation: 217

Alternatively, you may use Monolog-Cascade to do this.

Monolog-Cascade is a Monolog extension that allows you to set up and configure multiple loggers and handlers from a single config file.

Here is a sample config file for Monolog-Cascade using Loggly. This would log to you stdOut and to Loggly:

---
handlers:
    console:
        class: Monolog\Handler\StreamHandler
        level: DEBUG
        stream: php://stdout
    error_loggly_handler:
        class: Monolog\Handler\LogglyHandler
        level: ERROR
        token: xxxx-xxxx-xxxxxxxx
        tags: [cascade, waterfall]
loggers:
    my_logger:
        handlers: [console, error_loggly_handler]

If you're interested, here is a blog post on Cascade => https://medium.com/orchard-technology/enhancing-monolog-699efff1051d


[Disclaimer]: I am the main contributor of Monolog-Cascade.

Upvotes: 1

Nerman
Nerman

Reputation: 169

To expand on Hassan's contribution (posting as an answer, as I still don't have enough reputation to post a comment).

If you have a need to use daily logs locally, you could use following code:

$logFile = 'laravel'.'.txt';
$log->useDailyFiles(storage_path().'/logs/'.$logFile);

Of course, logfile name is totally arbitrary. In this example, format will be as such:

laravel-YYYY-MM-DD.txt

Edit: with an upgrade to 5.4 this line does not work anymore:

$log = $app->make(Illuminate\Log\Writer::class);

As a workaround, you can create Writer instance manually, injecting $monolog available from configureMonologUsing closure:

$log = new Illuminate\Log\Writer($monolog);

Upvotes: 0

hasusuf
hasusuf

Reputation: 1589

I was able to manage having Laravel's default local log behaviour, and pushing to Loggly in the same time, by tweaking mladen-janjetovic's code a bit. Tested on Laravel 5.3

config/services.php:

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

bootstrap/app.php:

/*
|--------------------------------------------------------------------------
| Push to Loggly, and save locally.
|--------------------------------------------------------------------------
*/
$app->configureMonologUsing(function($monolog) use ($app) {
    $log = $app->make(Illuminate\Log\Writer::class);

    $logglyHandler = new \Monolog\Handler\LogglyHandler(config('services.loggly.key'));
    $logglyHandler->setTag(config('services.loggly.tag'));

    if (config('app.env') == 'production') 
    {
        // Push to Loggly and save local if in production 
        $log->getMonolog()->pushHandler($logglyHandler);
        $log->useFiles(storage_path('/logs/laravel.log'));
    }
    else
    {
        // Otherwise, save only locally
        $log->useFiles(storage_path('/logs/laravel.log'));
    }
});

Upvotes: 2

Related Questions