15

I am trying to log an array with monolog in symfony.

$logger = $this->get('logger');
$logger->info(=print_R($user,true));

the output i get is not formatted as a print_r would be expected. It logs it all on one line.

I do not have any monolog settings in my config.yml and suspect this may be the issue.

How can I log a print_r(array) using monolog so it displays formatted in a tail -f?

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
Chris
  • 2,166
  • 1
  • 24
  • 37
  • 1
    Have you looked here http://symfony.com/doc/current/reference/configuration/monolog.html – B Rad Mar 28 '15 at 05:00

2 Answers2

21

Monolog uses Monolog\Formatter\LineFormatter by default without any arguments. Formatter is basically object that is responsible for final output in your logs. Look at constructor definition:

public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false)

As you can see LineFormatter creates one line from your print_r output because of third argument. You need to define new service with custom arguments for LineFormatter.

# app/config/services.yml - for example
services:
    monolog.my_line_formatter: # Your name
        class: Monolog\Formatter\LineFormatter
        arguments: [~, ~, true]

Now find your monolog definition and use formatter for what you need.

# Example from default config_dev.yml
monolog:
    handlers:
        main:
            type:   stream
            path:   "%kernel.logs_dir%/%kernel.environment%.log"
            level:  debug
            formatter: monolog.my_line_formatter
kba
  • 4,190
  • 2
  • 15
  • 24
  • Thanks. The documentation would do well to add something so clear and straightforward. – Chris Mar 28 '15 at 12:22
  • 3
    This is a great answer but look at this. you need to invoke a service across 2 separate configurations in order to echo newlines to a log file. And as @Chris mentioned, zero documentation on how this should be done. log4j, log4php, railslogger etc. typically support newlines and I've never had to write more than one or two configuration lines, let alone invoke a who knows what service to simply echo perfectly acceptable ascii newlines. Holy cow. – eggmatters Sep 29 '16 at 21:11
  • 1
    This is more about Monolog library than Symfony. Symfony allows you configure Monolog in way it's actually working as standalone library. If you know Monolog and Symfony service container then this solution is very clear and it's even in [doc](http://symfony.com/doc/current/logging/formatter.html). – kba Sep 30 '16 at 14:50
4

You will need to use use Monolog\Formatter\LineFormatter to overwrite default settings for the log message formatter. Below is code:

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

$logFilePath = __DIR__.'/path/to/logFile.log';
$arr = array('abc' => 'xyz', 'qwerty' => 'yuiop', 'username' => 'abc xyz');

$logger = new Logger('my_logger');

$formatter = new LineFormatter(
    null, // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n
    null, // Datetime format
    true, // allowInlineLineBreaks option, default false
    true  // discard empty Square brackets in the end, default false
);

// Debug level handler
$debugHandler = new StreamHandler($logFilePath, Logger::DEBUG);
$debugHandler->setFormatter($formatter);

$logger->pushHandler($debugHandler);

$logger->info('FORMATTED ARRAY WITH MULTI-LINE');
$logger->info(print_r($arr, true));

Below is the Log message written to the log file:

[2019-06-06 09:24:05] my_logger.INFO: FORMATTED ARRAY WITH MULTI-LINE  
[2019-06-06 09:24:05] my_logger.INFO: Array
(
    [abc] => xyz
    [qwerty] => yuiop
    [username] => abc xyz
)
Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69