if code write as follow, it will work:
use std::time::Duration;
use tokio::time::sleep;
use tracing::{info, instrument};
#[tokio::main]
async fn main() {
let file_writer = tracing_appender::rolling::daily("logs", "log.txt");
let (non_blocking_writer, _guard) = tracing_appender::non_blocking(file_writer);
tracing_subscriber::fmt()
.with_writer(non_blocking_writer)
.with_ansi(false)
.init();
wait().await;
info!("Starting server...")
}
#[instrument]
async fn wait() {
sleep(Duration::from_secs(1)).await;
info!("record after 1s");
}
but if i create a function to wrap tracing init,it will not work:
use std::time::Duration;
use tokio::time::sleep;
use tracing::{info, instrument};
#[tokio::main]
async fn main() {
init();
wait().await;
info!("Starting server...")
}
#[instrument]
async fn wait() {
sleep(Duration::from_secs(1)).await;
info!("record after 1s");
}
fn init() {
let file_writer = tracing_appender::rolling::daily("logs", "log.txt");
let (non_blocking_writer, _guard) = tracing_appender::non_blocking(file_writer);
tracing_subscriber::fmt()
.with_writer(non_blocking_writer)
.with_ansi(false)
.init();
}
what problem,it‘s lifetime problem? when the function excuted ,this scope will drop? if i still want wrap it into a function ,how to modify it.