#tracing #log-file #logging-tracing #rolling #logging #date-format

tracing-rolling

helper crate to customize rolling log file with tracing crate

3 releases (breaking)

0.3.0 Dec 8, 2023
0.2.0 Dec 7, 2023
0.1.0 Mar 30, 2023

#919 in Debugging

Download history 29/week @ 2024-09-14 55/week @ 2024-09-21 40/week @ 2024-09-28 25/week @ 2024-10-05 23/week @ 2024-10-12 26/week @ 2024-10-19 37/week @ 2024-10-26 33/week @ 2024-11-02 12/week @ 2024-11-09 41/week @ 2024-11-16 66/week @ 2024-11-23 38/week @ 2024-11-30 54/week @ 2024-12-07 38/week @ 2024-12-14 16/week @ 2024-12-21 12/week @ 2024-12-28

125 downloads per month
Used in hdfs-fuse

Custom license

14KB
330 lines

tracing-rolling

helper crate to customize rolling log file with tracing crate

use std::time::Duration;

use time::macros::offset;
use tokio::time::sleep;
use tracing::info;
use tracing_rolling::{Checker, Daily};

#[tokio::main]
async fn main() {
    // create a daily rolling file with custom date format
    // output file pattern is testing.20230323.log
    let (writer, token) = Daily::new("logs/testing.log", "[year][month][day]", offset!(+8))
        // Daily::new("logs/testing.log", None, offset!(+8))
        .buffered() // buffer file if needed
        .build()
        .unwrap();

    tracing_subscriber::fmt()
        .with_ansi(false)
        .with_target(false)
        .with_file(true)
        .with_line_number(true)
        .with_writer(writer)
        .init();
    let mut count = 0;
    info!("start");
    while count < 100 {
        count += 1;
        sleep(Duration::from_millis(50)).await;
        info!("{count}");
    }
    // have to manual drop to flush writer when process exit
    drop(token);
}

Dependencies

~4.5–10MB
~100K SLoC