How to send a log event from Laravel to Loggly?

I want to send Monolog logs from my Laravel 5.1 application to Loggly.com log management service. Of all the possible conditions, including local development.

+4
source share
4 answers

I found some outdated libraries and complicated ways to do this. So I ended up with a very simple solution. In fact, the Laravel Monolog Handler already has a Loggly handler out of the box.

Add configuration information to config / services.php:

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

Then add the Monolog handler to bootstrap / app.php, before returning $ app:

/*
|--------------------------------------------------------------------------
| 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 get your Monolog logs in the Loggly toolbar.

+6
source

Laravel Loggly, mladen-janjetovic . Laravel 5.3

/services.php:

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

/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'));
    }
});
+2

( , ).

, :

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

, . :

laravel-YYYY-MM-DD.txt

: 5.4 :

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

Writer , $monolog configureMonologUsing :

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

Monolog-Cascade .

Monolog-Cascade - Monolog, .

Monolog-Cascade Loggly. stdOut 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]

, Cascade = > https://medium.com/orchard-technology/enhancing-monolog-699efff1051d


[ ]: Monolog-Cascade.

0

All Articles