I am trying to log an array with monolog in symfony.
我试图在symfony中使用monolog记录一个数组。
$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.
我得到的输出没有格式化为print_r预期。它将所有内容记录在一行上。
I do not have any monolog settings in my config.yml and suspect this may be the issue.
我的config.yml中没有任何monolog设置,并怀疑这可能是问题所在。
How can I log a print_r(array) using monolog so it displays formatted in a tail -f?
如何使用monolog记录print_r(数组),以便显示格式为tail -f?
1 个解决方案
#1
15
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:
Monolog默认使用Monolog \ Formatter \ LineFormatter而不带任何参数。 Formatter基本上是负责日志中最终输出的对象。看一下构造函数定义:
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
.
正如您所看到的,由于第三个参数,LineFormatter会从print_r输出创建一行。您需要使用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.
现在找到你的monolog定义并使用formatter来满足你的需求。
# 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
#1
15
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:
Monolog默认使用Monolog \ Formatter \ LineFormatter而不带任何参数。 Formatter基本上是负责日志中最终输出的对象。看一下构造函数定义:
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
.
正如您所看到的,由于第三个参数,LineFormatter会从print_r输出创建一行。您需要使用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.
现在找到你的monolog定义并使用formatter来满足你的需求。
# 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