How to install a rolling file and console log as the global default. #2481
Answered
by
davidbarsky
ChaseLewis
asked this question in
Q&A
-
The documentation is incredibly confusing on how to set the global default manually & how to put multiple writers on it.
This works fine if if I just call tracing_subscriber::fmt::init() instead so the configuration is the issue. Currently it just makes the rolling file and doesn't write anything to it. |
Beta Was this translation helpful? Give feedback.
Answered by
davidbarsky
Feb 21, 2023
Replies: 1 comment 3 replies
-
You're not returning the You'll need to rewrite your code as: pub fn initialize_logger(config: &LoggingConfig) -> WorkerGuard {
let file_appender = tracing_appender::rolling::daily(config.output_path.to_owned(),config.filename.to_owned());
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
tracing_subscriber::fmt()
.with_writer(non_blocking)
.init();
guard
}
#[tokio::main]
async fn main() -> tokio::io::Result<()> {
let guard = initialize_logger(&Default::default());
// ... the rest of your main function
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
ChaseLewis
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're not returning the
guard
ininitialize_logger
. You need to return it and hold it as long as you'd like to log to a file. Take a look at theWorkerGuard
's documentation for more details.You'll need to rewrite your code as: