Skip to content

A rust crate that implements a simple time or size-based rolling file appender.

License

Notifications You must be signed in to change notification settings

WANG-lp/local-rolling-file

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

local-rolling-file

local-rolling-file on GitHub Actions local-rolling-file on crates.io rolling-file on docs.rs GitHub: WANG-lp/local-rolling-file license: MIT or Apache-2.0 minimum rustc: 1.42

NOTE: this repo is based on https://github.com/Axcient/rolling-file-rs

A rolling file appender with customizable rolling conditions. Includes built-in support for rolling conditions on date/time (daily, hourly, every minute) and/or size.

Log files structures(with log as folder and log.log as prefix):

  • log.log (a symbol link always points to the latest one log file)
  • log.log.yyyymmdd.hhmmss (e.g. log.log.20240520.010101)
  • ..

This is useful to combine with the tracing crate and tracing_appender::non_blocking::NonBlocking -- use it as an alternative to tracing_appender::rolling::RollingFileAppender.

Examples

use rolling_file::*;
let file_appender = BasicRollingFileAppender::new(
    "./log", // folder
    "log.log", // prefix
    RollingConditionBasic::new().daily(),
    9
).unwrap();
let file_appender = local_rolling_file::RollingFileAppender::new(
        folder,
        "log.log",
        local_rolling_file::RollingConditionBasic::new().daily(),
        3,
    )
    .unwrap();
    let (log_file_writer, guard) = tracing_appender::non_blocking(file_appender);
    let local_time = tracing_subscriber::fmt::time::OffsetTime::new(
        UtcOffset::from_hms(8, 0, 0).unwrap(),
        format_description!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:6]"),
    );
    let subscriber = tracing_subscriber::Registry::default()
        .with(
            tracing_subscriber::fmt::layer()
                .compact()
                .with_target(false)
                .with_file(true)
                .with_line_number(true)
                .with_ansi(false)
                .with_timer(local_time.clone())
                .with_writer(log_file_writer)
                .with_filter(level),
        )
        .with(
            tracing_subscriber::fmt::layer()
                .compact()
                .with_target(false)
                .with_file(true)
                .with_line_number(true)
                .with_ansi(true)
                .with_timer(local_time)
                .with_filter(level),
        );

    // use that subscriber to process traces emitted after this point
    let _ = tracing::subscriber::set_global_default(subscriber);
    tracing::info!("Logger set up successfully");

Development

Must pass latest stable clippy, be formatted with nightly rustfmt, and pass unit tests:

cargo +nightly fmt
cargo clippy --all-targets
cargo test

License

Dual-licensed under the terms of either the MIT license or the Apache 2.0 license.

Changelog

See CHANGELOG.md

About

A rust crate that implements a simple time or size-based rolling file appender.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%