phplaravelmonologloggly

How can I send json formatted log entries to Loggly with Laravel 5.6


I've followed what I think is the correct setup for Loggly as a Monolog channel with Laravel 5.6 but I keep getting strings of text instead of json searchable entries. E.g:

[2018-07-27 14:50:20] local.INFO: array ( 'foo' => 'bar', )

This is from the 'unparsed' property of the Loggly entry. Not a valid json by any stretch!!!

Here are some relevant snippets of code to show how the loggly channel is set up and how I trigger the log entry.

config/logging.php:

'loggly' => [
        'driver' => 'monolog',
        'handler' => Monolog\Handler\LogglyHandler::class,
        'with' => [
            'token' => env('LOGGLY_KEY'),
            'tag' => str_replace(' ', '_', env('APP_NAME') . '_' . env('APP_ENV')),
        ]
    ]

routes:

Route::get('/', function(){
Log::info(['foo' => 'bar']);
return view('welcome');

});

from composer.json:

"laravel/framework": "5.6.21",
"monolog/monolog": "^1.23",

I thought maybe upgrading to a more recent monolog version might help but ran into this when trying to update monolog via composer:

Your requirements could not be resolved to an installable set of packages.

Problem 1

  • laravel/framework v5.6.21 requires monolog/monolog ~1.12 -> satisfiable by monolog/monolog[1.x-dev].
  • laravel/framework v5.6.21 requires monolog/monolog ~1.12 -> satisfiable by monolog/monolog[1.x-dev].
  • laravel/framework v5.6.21 requires monolog/monolog ~1.12 -> satisfiable by monolog/monolog[1.x-dev].
  • Can only install one of: monolog/monolog[2.x-dev, 1.x-dev].
  • Installation request for monolog/monolog ^2.0 -> satisfiable by monolog/monolog[2.x-dev].
  • Installation request for laravel/framework 5.6.21 -> satisfiable by laravel/framework[v5.6.21].

What am I missing? Shouldn't Monolog be sending my log output as json to Loggly? Looking atLogglyHandler it uses LogglyFormatter which extends JsonFormatter. That seems sensible but in the end the output is a string. What gives?


Solution

  • After diving deep into monolog and json formatter the fix ended up being really simple. The first parameter is a text value and the second parameter can contain an associative array that gets translated to json and is searchable in Loggly.

    Log::info('Home Page Loaded',['foo' => 'bar']);