9

I'm newish to Laravel, and I've noticed when I use its Monolog-based logging, e.g. Log::info('blah blah'), the lines it writes to my logfile are suffixed with two empty sets of square brackets. What are they for, and how can I turn them off? They're not helpful in the slightest. I've tried digging into the source code and Googling a bit, but I'm not seeing any explanation.

Example:

[2013-11-12 09:13:16] log.INFO: Hello world [] []

[2013-11-12 09:13:31] log.INFO: My silly log message [] []

Thanks!

Community
  • 1
  • 1
curtisdf
  • 4,130
  • 4
  • 33
  • 42
  • 3
    So, it looks like I can pass an array of parameters as a 2nd argument to `Log::info()` and friends, which ends up showing in serialized form between the first set of brackets. And the second set appears to be for extra parameters that may be added by the logging system. But is there an easy way to make the brackets only appear when they have something in them, short of subclassing parts of Monolog? – curtisdf Nov 12 '13 at 17:38
  • The [docs](https://github.com/Seldaek/monolog/blob/master/doc/usage.md#adding-extra-data-in-the-records) describe these `context` and `extra` parameters. – TachyonVortex Jan 09 '15 at 13:14

3 Answers3

8

Here's a solution which combines curtisdf's answer with this answer by Seldaek, and doesn't require subclassing Monolog's LineFormatter.

Assuming your app/start/global.php file contains:

Log::useFiles(storage_path() . '/logs/laravel.log');

Replace that with:

use Monolog\Handler\StreamHandler;
use Monolog\Logger as MonologLogger;
use Monolog\Formatter\LineFormatter;

// Use custom LineFormatter, with ignoreEmptyContextAndExtra enabled
Log::getMonolog()->pushHandler(
    (new StreamHandler(
        storage_path() . '/logs/laravel.log',
        MonologLogger::DEBUG
    ))->setFormatter(new LineFormatter(null, null, true, true))
);
Community
  • 1
  • 1
TachyonVortex
  • 8,242
  • 3
  • 48
  • 63
5

After further research, I see this happens in Monolog\Formatter\LineFormatter. In short, the brackets are a JSON representation of an empty array. Unfortunately, it doesn't appear that Laravel provides an easy way out of this one, so I ended up subclassing Monolog. I posted the details at this SO post. But since that post is not Laravel-specific, I thought I'd share how to use such a custom LineFormatter in a Laravel context.

In your app/start/global.php, by default there's a section that defines logging. It looks like this:

Log::useDailyFiles(storage_path().'/logs/log-'.php_sapi_name().'.txt');

Replace it with this:

$handler = new Monolog\Handler\StreamHandler(
    storage_path().'/logs/log-'.php_sapi_name().'.txt');
$handler->setFormatter(new My\Fancy\Monolog\LineFormatter());
Log::getMonolog()->pushHandler($handler);

Enjoy!

Community
  • 1
  • 1
curtisdf
  • 4,130
  • 4
  • 33
  • 42
0

If you don't mind adding [] as second parameter to your calls to Log::, then it won't be printed:

Log::info('My funky log', []);

prints as:

My funky log

This usage also reminds you have option to pass an object in that array, and it will be dumped, if you want that.

mwal
  • 2,803
  • 26
  • 34