Question

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!

Was it helpful?

Solution 2

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!

OTHER TIPS

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))
);

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top