如何一次性将 log::Record 写入 csv::Writer 中?
Log
特征中定义的 log
函数接受 Record
参数。 据我所知,为了获取字符串形式的有效负载,我需要执行以下操作:
let mut s = String::new();
write!(s, "{}", record.args());
下一步是使用 csv::Writer:
let mut writer = csv::Writer::from_path("/path/to/csv");
writer.write_field(s);
// optionally write more fields
最简单的方法是什么一次通过?
The log
function defined in the Log
trait accepts a Record
parameter.
As far as I can tell, in order to obtain the payload as a string, I need to do something like:
let mut s = String::new();
write!(s, "{}", record.args());
The next step is to use csv::Writer
:
let mut writer = csv::Writer::from_path("/path/to/csv");
writer.write_field(s);
// optionally write more fields
What would be the simplest way to do that in a single pass?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
record.args()
返回&std::fmt::Arguments
,它实现了Display
特征,以及csv::Writer::write_field
接受一个字段
实施的AsRef<[u8]>
。所以这是有效的:record.args()
returns&std::fmt::Arguments
, it implementedDisplay
trait, andcsv::Writer::write_field
accepts afield
that implementedAsRef<[u8]>
. So this is working: