Skip to content

Commit 16e259e

Browse files
authored
Implement a new rotation policy Period (#74)
1 parent 4ab6ee2 commit 16e259e

File tree

5 files changed

+183
-26
lines changed

5 files changed

+183
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Fast, highly configurable Rust logging crate, inspired by the C++ logging librar
1717
- manually implementing for more flexibility.
1818
- Various combinable sinks:
1919
- standard streams with optional color support;
20-
- files (single file, rotating hourly, daily or by file size);
20+
- files (single file, rotating hourly, daily, periodically or by file size);
2121
- platform-specific (e.g. `journald` for Linux and `OutputDebugStringW` for Windows);
2222
- ... and able to implement one yourself.
2323
- Configuring via environment variables or TOML[^1].

spdlog/benches/spdlog-rs/spdlog_rs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ fn bench_rotating_inner(bencher: &mut Bencher, rotation_policy: RotationPolicy)
100100
RotationPolicy::FileSize(_) => "rotating_file_size",
101101
RotationPolicy::Daily { .. } => "rotating_daily",
102102
RotationPolicy::Hourly => "rotating_hourly",
103+
RotationPolicy::Period { .. } => "rotating_period",
103104
}))
104105
.rotation_policy(rotation_policy)
105106
.max_files(common::ROTATING_FILES)

spdlog/examples/02_file.rs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, sync::Arc};
1+
use std::{env, sync::Arc, time::Duration};
22

33
use spdlog::{
44
prelude::*,
@@ -7,7 +7,10 @@ use spdlog::{
77

88
fn main() -> Result<(), Box<dyn std::error::Error>> {
99
configure_file_logger()?;
10-
configure_rotating_file_logger()?;
10+
configure_rotating_daily_file_logger()?;
11+
configure_rotating_size_file_logger()?;
12+
configure_rotating_hourly_file_logger()?;
13+
configure_rotating_period_file_logger()?;
1114

1215
Ok(())
1316
}
@@ -24,8 +27,8 @@ fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
2427
Ok(())
2528
}
2629

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

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

39-
info!("this log will be written to the file `rotating.log`, and the file will be rotated daily at 00:00");
42+
info!("this log will be written to the file `rotating_daily.log`, and the file will be rotated daily at 00:00");
43+
44+
Ok(())
45+
}
46+
47+
fn configure_rotating_size_file_logger() -> Result<(), Box<dyn std::error::Error>> {
48+
let path = env::current_exe()?.with_file_name("rotating_size.log");
49+
50+
let file_sink = Arc::new(
51+
RotatingFileSink::builder()
52+
.base_path(path)
53+
.rotation_policy(RotationPolicy::FileSize(1024))
54+
.build()?,
55+
);
56+
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
57+
spdlog::set_default_logger(new_logger);
58+
59+
info!("this log will be written to the file `rotating_size.log`, and the file will be rotated when its size reaches 1024 bytes");
60+
61+
Ok(())
62+
}
63+
64+
fn configure_rotating_hourly_file_logger() -> Result<(), Box<dyn std::error::Error>> {
65+
let path = env::current_exe()?.with_file_name("rotating_hourly.log");
66+
67+
let file_sink = Arc::new(
68+
RotatingFileSink::builder()
69+
.base_path(path)
70+
.rotation_policy(RotationPolicy::Hourly)
71+
.build()?,
72+
);
73+
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
74+
spdlog::set_default_logger(new_logger);
75+
76+
info!("this log will be written to the file `rotating_hourly.log`, and the file will be rotated every hour");
77+
78+
Ok(())
79+
}
80+
81+
fn configure_rotating_period_file_logger() -> Result<(), Box<dyn std::error::Error>> {
82+
let path = env::current_exe()?.with_file_name("rotating_period.log");
83+
84+
let file_sink = Arc::new(
85+
RotatingFileSink::builder()
86+
.base_path(path)
87+
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
88+
60 * 90, // 90 minutes
89+
)))
90+
.build()?,
91+
);
92+
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?);
93+
spdlog::set_default_logger(new_logger);
94+
95+
info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours");
4096

4197
Ok(())
4298
}

0 commit comments

Comments
 (0)