use std::{env, time::Duration};
use spdlog::{
prelude::*,
sink::{FileSink, RotatingFileSink, RotationPolicy},
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
configure_file_logger()?;
configure_rotating_daily_file_logger()?;
configure_rotating_size_file_logger()?;
configure_rotating_hourly_file_logger()?;
configure_rotating_period_file_logger()?;
Ok(())
}
fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> {
let path = env::current_exe()?.with_file_name("file.log");
let file_sink = FileSink::builder().path(path).build_arc()?;
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
spdlog::set_default_logger(new_logger);
info!("this log will be written to the file `all.log`");
Ok(())
}
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 = RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
.build_arc()?;
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
spdlog::set_default_logger(new_logger);
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 = RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::FileSize(1024))
.build_arc()?;
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
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 = RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Hourly)
.build_arc()?;
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
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 = RotatingFileSink::builder()
.base_path(path)
.rotation_policy(RotationPolicy::Period(Duration::from_secs(
60 * 90, )))
.build_arc()?;
let new_logger = Logger::builder().sink(file_sink).build_arc()?;
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(())
}