如何像格式数组一样用monolog记录多行条目? [英] How to log multiline entries with monolog like a formatted array?
问题描述
我正在尝试在symfony
中用monolog
记录一个数组.
I am trying to log an array with monolog
in symfony
.
$logger = $this->get('logger');
$logger->info(=print_R($user,true));
我得到的输出未格式化为预期的print_r.它全部记录在一行上.
the output i get is not formatted as a print_r would be expected. It logs it all on one line.
我的config.yml
中没有任何独白设置,并且怀疑这可能是问题所在.
I do not have any monolog settings in my config.yml
and suspect this may be the issue.
如何使用独白记录print_r(array)
,以便它以tail -f
格式显示?
How can I log a print_r(array)
using monolog so it displays formatted in a tail -f
?
推荐答案
Monolog默认使用Monolog\Formatter\LineFormatter
,不带任何参数.格式化程序基本上是负责最终输出日志的对象.查看构造函数定义:
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)
如您所见,由于第三个参数,LineFormatter从print_r输出创建了一行.您需要使用LineFormatter
的自定义参数定义新服务.
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
这篇关于如何像格式数组一样用monolog记录多行条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!