Crates.io | local-rolling-file |
lib.rs | local-rolling-file |
version | 1.1.1 |
source | src |
created_at | 2024-05-21 03:20:31.767654 |
updated_at | 2024-05-21 03:34:41.70853 |
description | A rolling file appender with customizable rolling conditions. |
homepage | https://github.com/WANG-lp/local-rolling-file |
repository | https://github.com/WANG-lp/local-rolling-file |
max_upload_size | |
id | 1246383 |
size | 32,112 |
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):
(a symbol link always points to the latest one log file)
(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.
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");
Must pass latest stable clippy, be formatted with nightly rustfmt, and pass unit tests:
cargo +nightly fmt
cargo clippy --all-targets
cargo test
Dual-licensed under the terms of either the MIT license or the Apache 2.0 license.
See CHANGELOG.md