Php 如何不显示独白日志行中的最后一个括号?
生成日志文件LaurentCommand.log: LaurentCommand.INFO:Start命令{“用户名”:“乔”,“年龄”:“28”}[]Php 如何不显示独白日志行中的最后一个括号?,php,symfony,monolog,Php,Symfony,Monolog,生成日志文件LaurentCommand.log: LaurentCommand.INFO:Start命令{“用户名”:“乔”,“年龄”:“28”}[] 为什么要在末尾加上这个括号?这是额外的数据。默认格式为“[%datetime%]%channel%。%level\u name%:%message%%context%%extra%\n”。用户名/年龄是上下文,通常为空的额外值将导致此空数组[] 如果使用处理器将数据附加到日志记录,它们通常会将数据写入额外的键,以避免与上下文信息冲突。如果这确实
为什么要在末尾加上这个括号?这是额外的数据。默认格式为
“[%datetime%]%channel%。%level\u name%:%message%%context%%extra%\n”
。用户名/年龄是上下文,通常为空的额外值将导致此空数组[]
如果使用处理器将数据附加到日志记录,它们通常会将数据写入额外的键,以避免与上下文信息冲突。如果这确实是一个问题,您可以更改默认格式并省略%extra%
编辑:从Monolog 1.11开始,LineFormatter在构造函数中有一个$ignoreEmptyContextAndExtra参数,允许您删除这些参数,因此您可以使用此参数:
// 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'));
我知道这是一个老问题,但我也遇到了它,我想分享我的解决方案 日志行末尾的括号是由于Monolog的
LineFormatter
试图json\u encode()
在%extra%
中对数据进行编码。括号是空数组的JSON表示形式
要关闭这些括号,我必须用自己的类对Monolog\Formatter\LineFormatter
进行子类化,并覆盖其convertToString($data)
函数,以便在没有数据时返回空字符串。这是我的新子类:
// the last "true" here tells it to remove empty []'s
$formatter = new LineFormatter(null, null, false, true);
$handler->setFormatter($formatter);
您可以通过将该类的实例注入到Monolog处理程序类中来使用该类,如下所示:
namespace My\Fancy\Monolog;
use Monolog\Formatter\LineFormatter;
class LineFormatter extends LineFormatter {
protected function convertToString($data)
{
if (null === $data || is_scalar($data)) {
return (string) $data;
}
// BEGIN CUSTOM CODE - This section added to prevent empty
// brackets from appearing at the end of log lines:
if ((is_array($data) && !$data)
|| is_object($data) && !get_object_vars($data)) {
return '';
}
// END CUSTOM CODE
$data = $this->normalize($data);
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return $this->toJson($data);
}
return str_replace('\\/', '/', json_encode($data));
}
}
享受吧 老问题,但抛出另一个简单选项:
$handler = new Monolog\Handler\StreamHandler('/path/to/my/logfile', 'debug');
$handler->setFormatter(new My\Fancy\Monolog\LineFormatter());
$monolog->pushHandler($handler);
Symfony 4解决方案:
$slackHandler = new \Monolog\Handler\SlackWebhookHandler(...);
$slackHandler->getFormatter()->ignoreEmptyContextAndExtra(true);
服务.yml
中定义格式化程序:
use Monolog\Formatter\LineFormatter;
class Formatter extends LineFormatter
{
public function __construct(
$format = null,
$dateFormat = null,
$allowInlineLineBreaks = false,
$ignoreEmptyContextAndExtra = false
) {
parent::__construct($format, $dateFormat, $allowInlineLineBreaks, true);
}
}
monolog.yml
中为所需环境定义格式化程序:
Seldaek是指
$log=newlogger('LaurentCommand')$handler=newstreamhandler('./app/logs/LaurentCommand.log')$处理程序->设置格式化程序(新的行格式化程序([%datetime%]%channel%.%level_name%:%message%%context%\n”)$log->pushHandler($handler)$log->addInfo(“开始命令”,数组('username'=>'Joe','Age'=>'28')代码>就像一个符咒。谢谢:)
log.custom.formatter:
class: App\Formatter
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
formatter: log.custom.formatter