Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

php - How not to show last bracket in a monolog log line?

// 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'));

Result in log file LaurentCommand.log :

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

Why this bracket at the end ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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%.

Edit: 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...