1

Now my code looks like this:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$ipAddress = GeoIp\Manager::getRealIp();
$geoResult = GeoIp\Manager::getDataResult($ipAddress, 'ru');

$logger = new Logger('Debug');
$logger->pushHandler(new StreamHandler($_SERVER['DOCUMENT_ROOT'].'/logs/geo.log', Logger::DEBUG));
$logger->info('Location:', array('object' => print_r($geoResult, true)));

As a result I have one-line log entry. I tried to use JsonFormatter, but is just formats the data structure and continues print one-line records, when I want a few-line human readable beautified presentation of data.

UPDATE:

I found solution here later. Symfony2 - How to log multiline entries with monolog? And for me it looks like this:

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

$ipAddress = GeoIp\Manager::getRealIp();
$geoResult = GeoIp\Manager::getDataResult($ipAddress, 'ru');

$formatter = new LineFormatter(null, null, true);

$stream = new StreamHandler($_SERVER['DOCUMENT_ROOT'].'/logs/geo.log', Logger::DEBUG);
$stream->setFormatter($formatter);

$logger = new Logger('Debug');
$logger->pushHandler($stream);
$logger->info('Location:', array('object' => print_r($geoResult, true)));
Ildar Meyker
  • 57
  • 1
  • 5

2 Answers2

0

You can use json_encode with JSON_PRETTY_PRINT flat as following:-

$beautifiedJsonObjectString = json_encode($data, JSON_PRETTY_PRINT);
// now you can use the formatted objected as you like

Reference: https://www.daveperrett.com/articles/2008/03/11/format-json-with-php/

nandal
  • 2,544
  • 1
  • 18
  • 23
  • It have not worked. But I found the solution here. https://stackoverflow.com/questions/29312746/symfony2-how-to-log-multiline-entries-with-monolog?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Ildar Meyker May 23 '18 at 15:07
0

Since version 2.0.0 the Monolog\Formatter\JsonFormatter, or any formatter that extends Monolog\Formatter\NormalizerFormatter, supports this via setJsonPrettyPrint()

$formatter = new JsonFormatter();
$formatter->setJsonPrettyPrint(true);

There is also a more generic JSON encoding option if needed

$formatter->addJsonEncodeOption($option);
AlexP
  • 9,906
  • 1
  • 24
  • 43