How to log multiline entries with monolog like a formatted array?

前端 未结 2 754
悲&欢浪女
悲&欢浪女 2021-02-19 08:08

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

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


        
2条回答
  •  [愿得一人]
    2021-02-19 08:36

    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
    

提交回复
热议问题