Skip to content

Commit

Permalink
Implement a new rotation policy Period (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
lioriz authored Sep 30, 2024
1 parent 4ab6ee2 commit 16e259e
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Fast, highly configurable Rust logging crate, inspired by the C++ logging librar
- manually implementing for more flexibility.
- Various combinable sinks:
- standard streams with optional color support;
- files (single file, rotating hourly, daily or by file size);
- files (single file, rotating hourly, daily, periodically or by file size);
- platform-specific (e.g. `journald` for Linux and `OutputDebugStringW` for Windows);
- ... and able to implement one yourself.
- Configuring via environment variables or TOML[^1].
Expand Down
1 change: 1 addition & 0 deletions spdlog/benches/spdlog-rs/spdlog_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ fn bench_rotating_inner(bencher: &mut Bencher, rotation_policy: RotationPolicy)
RotationPolicy::FileSize(_) => "rotating_file_size",
RotationPolicy::Daily { .. } => "rotating_daily",
RotationPolicy::Hourly => "rotating_hourly",
RotationPolicy::Period { .. } => "rotating_period",
}))
.rotation_policy(rotation_policy)
.max_files(common::ROTATING_FILES)
Expand Down
66 changes: 61 additions & 5 deletions spdlog/examples/02_file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, sync::Arc};
use std::{env, sync::Arc, time::Duration};

use spdlog::{
prelude::*,
Expand All @@ -7,7 +7,10 @@ use spdlog::{

fn main() -> Result<(), Box<dyn std::error::Error>> {
configure_file_logger()?;
configure_rotating_file_logger()?;
configure_rotating_daily_file_logger()?;
configure_rotating_size_file_logger()?;
configure_rotating_hourly_file_logger()?;
configure_rotating_period_file_logger()?;

Ok(())
}
Expand All @@ -24,8 +27,8 @@ fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn configure_rotating_file_logger() -> Result<(), Box<dyn std::error::Error>> {
let path = env::current_exe()?.with_file_name("rotating.log");
fn configure_rotating_daily_file_logger() -> Result<(), Box<dyn std::error::Error>> {
let path = env::current_exe()?.with_file_name("rotating_daily.log");

let file_sink = Arc::new(
RotatingFileSink::builder()
Expand All @@ -36,7 +39,60 @@ fn configure_rotating_file_logger() -> Result<(), Box<dyn std::error::Error>> {
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
spdlog::set_default_logger(new_logger);

info!("this log will be written to the file `rotating.log`, and the file will be rotated daily at 00:00");
info!("this log will be written to the file `rotating_daily.log`, and the file will be rotated daily at 00:00");

Ok(())
}

fn configure_rotating_size_file_logger() -> Result<(), Box<dyn std::error::Error>> {
let path = env::current_exe()?.with_file_name("rotating_size.log");

let file_sink = Arc::new(
RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::FileSize(1024))
.build()?,
);
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
spdlog::set_default_logger(new_logger);

info!("this log will be written to the file `rotating_size.log`, and the file will be rotated when its size reaches 1024 bytes");

Ok(())
}

fn configure_rotating_hourly_file_logger() -> Result<(), Box<dyn std::error::Error>> {
let path = env::current_exe()?.with_file_name("rotating_hourly.log");

let file_sink = Arc::new(
RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Hourly)
.build()?,
);
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
spdlog::set_default_logger(new_logger);

info!("this log will be written to the file `rotating_hourly.log`, and the file will be rotated every hour");

Ok(())
}

fn configure_rotating_period_file_logger() -> Result<(), Box<dyn std::error::Error>> {
let path = env::current_exe()?.with_file_name("rotating_period.log");

let file_sink = Arc::new(
RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
60 * 90, // 90 minutes
)))
.build()?,
);
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
spdlog::set_default_logger(new_logger);

info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours");

Ok(())
}
Loading

0 comments on commit 16e259e

Please sign in to comment.