Skip to content

How to install a rolling file and console log as the global default. #2481

Answered by davidbarsky
ChaseLewis asked this question in Q&A
Discussion options

You must be logged in to vote

You're not returning the guard in initialize_logger. You need to return it and hold it as long as you'd like to log to a file. Take a look at the WorkerGuard's documentation for more details.

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());

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ChaseLewis
Comment options

@davidbarsky
Comment options

@ChaseLewis
Comment options

Answer selected by ChaseLewis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants