I know this is an old question, but I ran into it too and I want to share my solution.
The brackets at the end of log lines are due to how Monolog's LineFormatter
tries to json_encode()
the data in %extra%
. The brackets are a JSON representation of an empty array.
To turn off those brackets, I ended up having to subclass Monolog\Formatter\LineFormatter
with my own class and overwrite its convertToString($data)
function so it returns an empty string if there's no data present. Here's my new subclass:
namespace My\Fancy\Monolog;
use Monolog\Formatter\LineFormatter;
class LineFormatter extends LineFormatter {
protected function convertToString($data)
{
if (null === $data || is_scalar($data)) {
return (string) $data;
}
// BEGIN CUSTOM CODE - This section added to prevent empty
// brackets from appearing at the end of log lines:
if ((is_array($data) && !$data)
|| is_object($data) && !get_object_vars($data)) {
return '';
}
// END CUSTOM CODE
$data = $this->normalize($data);
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return $this->toJson($data);
}
return str_replace('\\/', '/', json_encode($data));
}
}
You can use this class by injecting an instance of it into your Monolog handler class, like so:
$handler = new Monolog\Handler\StreamHandler('/path/to/my/logfile', 'debug');
$handler->setFormatter(new My\Fancy\Monolog\LineFormatter());
$monolog->pushHandler($handler);
Enjoy!