1use std::{env, time::Duration};
2
3use spdlog::{
4 prelude::*,
5 sink::{FileSink, RotatingFileSink, RotationPolicy},
6};
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 configure_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()?;
14
15 Ok(())
16}
17
18fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
19 let path = env::current_exe()?.with_file_name("file.log");
20
21 let file_sink = FileSink::builder().path(path).build_arc()?;
22 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
23 spdlog::set_default_logger(new_logger);
24
25 info!("this log will be written to the file `all.log`");
26
27 Ok(())
28}
29
30fn configure_rotating_daily_file_logger() -> Result<(), Box<dyn std::error::Error>> {
31 let path = env::current_exe()?.with_file_name("rotating_daily.log");
32
33 let file_sink = RotatingFileSink::builder()
34 .base_path(path)
35 .rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
36 .build_arc()?;
37 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
38 spdlog::set_default_logger(new_logger);
39
40 info!("this log will be written to the file `rotating_daily.log`, and the file will be rotated daily at 00:00");
41
42 Ok(())
43}
44
45fn configure_rotating_size_file_logger() -> Result<(), Box<dyn std::error::Error>> {
46 let path = env::current_exe()?.with_file_name("rotating_size.log");
47
48 let file_sink = RotatingFileSink::builder()
49 .base_path(path)
50 .rotation_policy(RotationPolicy::FileSize(1024))
51 .build_arc()?;
52 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
53 spdlog::set_default_logger(new_logger);
54
55 info!("this log will be written to the file `rotating_size.log`, and the file will be rotated when its size reaches 1024 bytes");
56
57 Ok(())
58}
59
60fn configure_rotating_hourly_file_logger() -> Result<(), Box<dyn std::error::Error>> {
61 let path = env::current_exe()?.with_file_name("rotating_hourly.log");
62
63 let file_sink = RotatingFileSink::builder()
64 .base_path(path)
65 .rotation_policy(RotationPolicy::Hourly)
66 .build_arc()?;
67 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
68 spdlog::set_default_logger(new_logger);
69
70 info!("this log will be written to the file `rotating_hourly.log`, and the file will be rotated every hour");
71
72 Ok(())
73}
74
75fn configure_rotating_period_file_logger() -> Result<(), Box<dyn std::error::Error>> {
76 let path = env::current_exe()?.with_file_name("rotating_period.log");
77
78 let file_sink = RotatingFileSink::builder()
79 .base_path(path)
80 .rotation_policy(RotationPolicy::Period(Duration::from_secs(
81 60 * 90, )))
83 .build_arc()?;
84 let new_logger = Logger::builder().sink(file_sink).build_arc()?;
85 spdlog::set_default_logger(new_logger);
86
87 info!("this log will be written to the file `rotating_period.log`, and the file will be rotated every 1.5 hours");
88
89 Ok(())
90}