如何不在独白日志行中显示最后一个括号? [英] How not to show last bracket in a monolog log line?

查看:82
本文介绍了如何不在独白日志行中显示最后一个括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// in my PHP code
$log = new Logger('LaurentCommand');
$log->pushHandler(new StreamHandler('./app/logs/LaurentCommand.log'));
$log->addInfo("Start command",array('username' => 'Joe', 'Age' => '28'));

日志文件LaurentCommand.log中的结果:

Result in log file LaurentCommand.log :

[2012-12-20 10:28:11] LaurentCommand.INFO:启动命令{"username":"Joe","Age":"28"} []

[2012-12-20 10:28:11] LaurentCommand.INFO: Start command {"username":"Joe","Age":"28"} []

为什么将这个括号放在末尾?

Why this bracket at the end ?

推荐答案

这是额外的数据. LineFormatter 的默认格式为"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n".用户名/年龄是上下文,多余的(通常为空)会导致此空数组[].

That's the extra data. The default format of the LineFormatter is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n". the username/age is the context, and extra that is typically empty results in this empty array [].

如果使用处理器将数据附加到日志记录,则它们通常会将其写入额外的键,以避免与上下文信息冲突.如果确实有问题,可以更改默认格式并省略%extra%.

If you use processors to attach data to log records they typically write it to the extra key to avoid conflicts with context info. If it really is an issue for you you can change the default format and omit %extra%.

从Monolog 1.11开始,LineFormatter在构造函数中具有$ ignoreEmptyContextAndExtra参数,您可以删除这些参数,因此可以使用以下代码:

As of Monolog 1.11 the LineFormatter has a $ignoreEmptyContextAndExtra parameter in the constructor that lets you remove these, so you can use this:

// the last "true" here tells it to remove empty []'s
$formatter = new LineFormatter(null, null, false, true);
$handler->setFormatter($formatter);

这篇关于如何不在独白日志行中显示最后一个括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆