【发布时间】:2022-01-19 14:18:10
【问题描述】:
我正在尝试将旋转日志与fern 结合起来,通过将盒装FileRotate 实例(来自file-rotate 板条箱)与fern Dispatch 实例链接起来,但似乎无法满足编译器.这是代码sn-p:
let log= Box::new(
FileRotate::new(
"log/output.log",
CountSuffix::new(2),
ContentLimit::Lines(10),
Compression::None));
fern::Dispatch::new()
.level(LevelFilter::Debug)
.chain(log)
.apply()?;
编译器不同意,出现如下错误:
error[E0277]: the trait bound `fern::Output: From<Box<FileRotate<CountSuffix>>>` is not satisfied
--> src/main.rs:101:16
|
101 | .chain(log)
| ----- ^^^ the trait `From<Box<FileRotate<CountSuffix>>>` is not implemented for `fern::Output`
| |
| required by a bound introduced by this call
|
= help: the following implementations were found:
<fern::Output as From<&'static (dyn log::Log + 'static)>>
<fern::Output as From<Box<(dyn log::Log + 'static)>>>
<fern::Output as From<Box<(dyn std::io::Write + Send + 'static)>>>
<fern::Output as From<Dispatch>>
and 6 others
= note: required because of the requirements on the impl of `Into<fern::Output>` for `Box<FileRotate<CountSuffix>>`
note: required by a bound in `Dispatch::chain`
--> /Users/l203663/.cargo/registry/src/github.com-1ecc6299db9ec823/fern-0.6.0/src/builders.rs:195:21
|
195 | pub fn chain<T: Into<Output>>(mut self, logger: T) -> Self {
| ^^^^^^^^^^^^ required by this bound in `Dispatch::chain`
但是,FileRotate 结构体同时实现了Write 和Send,正如here 和here 所记录的那样。
编译器提出的最接近的实现是 IMO:
From<Box<(dyn std::io::Write + Send + 'static)>>
- 问题是由
'static生命周期引起的吗?在这种情况下,我该如何解决? - 如果不是,可能是什么原因造成的?
正在使用的箱子:
log = "0.4.14"
fern = "0.6.0"
file-rotate = "0.5.3"
【问题讨论】:
-
将
log的类型显式注释为Box<(dyn std::io::Write + Send + 'static)>是否提供任何有用的信息? -
感谢@SolomonUcko,这似乎有效。但是,我发现
as构造更清洁,如投票答案中所示。