我使用下面的代码来初始化我的跟踪。
我想只在某些情况下使用non_blocking
写入器,否则我只需要使用stdout
写入器。
如何避免下面的错误?
use tracing_appender::non_blocking;
use tracing_log::LogTracer;
use tracing_subscriber::{fmt::Layer, prelude::*, EnvFilter, Registry};
pub fn init(tracing: &Tracing) -> non_blocking::WorkerGuard {
LogTracer::init().expect("init LogTracer");
let (non_blocking, non_blocking_guard) = non_blocking(std::io::stdout());
let base_layer;
if tracing.blocking {
base_layer = Layer::default().with_writer(std::io::stdout);
} else {
base_layer = Layer::default().with_writer(non_blocking);
}
let mut layers = Vec::new();
layers.push(if tracing.pretty {
base_layer.pretty().with_file(false).boxed()
} else {
base_layer.json().boxed()
});
let registry = Registry::default()
.with(EnvFilter::from(&tracing.level))
.with(layers);
tracing::subscriber::set_global_default(registry).unwrap();
non_blocking_guard
}
错误:
error[E0308]: mismatched types
|
18 | base_layer = Layer::default().with_writer(non_blocking);
| ----------- ^^^^^^^^^^^^ expected fn item, found struct `tracing_appender::non_blocking::NonBlocking`
| |
| arguments to this function are incorrect
|
= note: expected fn item `fn() -> std::io::Stdout {std::io::stdout}`
found struct `tracing_appender::non_blocking::NonBlocking`
note: associated function defined here
|
177 | pub fn with_writer<W2>(self, make_writer: W2) -> Layer<S, N, E, W2>
| ^^^^^^^^^^^
您可以使用BoxMakeWriter
对写入器的具体类型进行类型擦除,以便稍后统一它们。我将像这样初始化你的跟踪:
use tracing_subscriber::fmt::writer::BoxMakeWriter;
let writer = if tracing.blocking {
BoxMakeWriter::new(std::io::stdout)
} else {
BoxMakeWriter::new(non_blocking)
};
let layer = if tracing.pretty {
Layer::default()
.with_writer(writer)
.pretty()
.with_file(false)
.boxed()
} else {
Layer::default()
.with_writer(writer)
.json()
.boxed()
};
let registry = Registry::default()
.with(EnvFilter::from(&tracing.level))
.with(layer);
注意:你不需要创建一个图层向量;.boxed()
的测定结果可以直接利用。