Skip to content

Per-test file logging #1626

Answered by hawkw
torsteingrindvik asked this question in Q&A
Oct 6, 2021 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

I can think of a couple approaches that might be useful:

Using the thread-local scoped dispatcher

One option, potentially the easiest, would just be to use the thread-local scoped trace dispatcher for each test, rather than initializing a global subscriber. You could write code like this:

use std::{fs::File, error::Error};
use tracing::dispatch::DefaultGuard;

fn init_tracing(test_name: &str) -> Result<DefaultGuard, Box<dyn Error>> {
    let file = File::create(format!("{}.log", test_name))?; // or however you want to name the log file

    let subscriber = tracing_subscriber::fmt()
        // ... however you would configure the subscriber goes here ...
        .with_writer(Arc::new(file));

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@hawkw
Comment options

@torsteingrindvik
Comment options

@hawkw
Comment options

@torsteingrindvik
Comment options

@cjwcommuny
Comment options

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