I have the following code:
use tracing::Level;
use tracing_subscriber::{
layer::{Layer, SubscriberExt},
filter,
util::SubscriberInitExt,
fmt::{ self, format::Writer, time::FormatTime }
};
use chrono::Local;
struct LocalTimer;
impl FormatTime for LocalTimer {
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
write!(w, "{}", Local::now().format("%Y-%m-%d %H:%M:%S"))
}
}
#[derive(Debug, Clone, Copy)]
pub enum Ticker {
A,
B,
C,
D,
}
fn main() {
let ticker_vec = vec![Ticker::A, Ticker::B, Ticker::C, Ticker::D];
let mut guard_vec = Vec::with_capacity(ticker_vec.len());
let layer_vec = ticker_vec
.into_iter()
.map(|ticker| {
let ticker_string = format!("{:?}", ticker);
let (non_blocking, guard) = tracing_appender::non_blocking(tracing_appender::rolling::daily(
"./logs",
ticker_string.clone(),
));
let filter_ticker = filter::filter_fn(move |metadata| {
metadata.target() == &ticker_string
});
let layer_ticker = fmt::layer()
.with_writer(non_blocking)
.with_filter(filter_ticker);
guard_vec.push(guard);
layer_ticker.boxed()
})
.collect::<Vec<_>>();
let json_layer = fmt::layer()
.with_ansi(false)
.with_file(true)
.with_line_number(true)
.with_timer(LocalTimer)
.json()
.flatten_event(true);
tracing_subscriber::registry()
.with(layer_vec)
.with(json_layer)
.init();
let ticker_vec_runtime = vec![Ticker::A, Ticker::C];
for ticker in ticker_vec_runtime.iter() {
tracing::info!(target: ticker, "run time ticker vec");
}
// let span = tracing::span!(Level::INFO, "aaa", value = ?ticker1);
// let _guard = span.enter();
// tracing::info!(target = ?ticker1, "Aaa1");
// tracing::info!(target = ?ticker2, "baa1");
// tracing::info!(target: "al", "Aaa");
// tracing::info!(target: "eb", "baa");
}
It has the following error:
attempt to use a non-constant value in a constant
non-constant valuerustcClick for full compiler diagnostic
macros.rs(855, 9): consider using `let` instead of `static`: `let __CALLSITE
The var ticker_vec_running
cannot be known at compile time. So how can I write the logs to different files in runtime?