pub struct RotatingFileSinkBuilder<ArgBP, ArgRP> { /* private fields */ }Expand description
Implementations§
Source§impl<ArgBP, ArgRP> RotatingFileSinkBuilder<ArgBP, ArgRP>
impl<ArgBP, ArgRP> RotatingFileSinkBuilder<ArgBP, ArgRP>
Sourcepub fn base_path<P>(
self,
base_path: P,
) -> RotatingFileSinkBuilder<PathBuf, ArgRP>
pub fn base_path<P>( self, base_path: P, ) -> RotatingFileSinkBuilder<PathBuf, ArgRP>
Specifies the base path of the log file.
The path needs to be suffixed with an extension, if you expect the rotated eventual file names to contain the extension.
If there is an extension, the different rotation policies will insert relevant information in the front of the extension. If there is not an extension, it will be appended to the end.
Supposes the given base path is /path/to/base_file.log, the eventual
file names may look like the following:
/path/to/base_file_1.log/path/to/base_file_2.log/path/to/base_file_2022-03-23.log/path/to/base_file_2022-03-24.log/path/to/base_file_2022-03-23_03.log/path/to/base_file_2022-03-23_04.log
This parameter is required.
Examples found in repository?
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, // 90 minutes
82 )))
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}Sourcepub fn rotation_policy(
self,
rotation_policy: RotationPolicy,
) -> RotatingFileSinkBuilder<ArgBP, RotationPolicy>
pub fn rotation_policy( self, rotation_policy: RotationPolicy, ) -> RotatingFileSinkBuilder<ArgBP, RotationPolicy>
Specifies the rotation policy.
This parameter is required.
Examples found in repository?
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, // 90 minutes
82 )))
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}Sourcepub fn max_files(self, max_files: usize) -> Self
pub fn max_files(self, max_files: usize) -> Self
Specifies the maximum number of files.
If the number of existing files reaches this parameter, the oldest file will be deleted on the next rotation.
Specify 0 for no limit.
This parameter is optional, and defaults to 0.
Sourcepub fn rotate_on_open(self, rotate_on_open: bool) -> Self
pub fn rotate_on_open(self, rotate_on_open: bool) -> Self
Specifies whether to rotate files once when constructing
RotatingFileSink.
For the RotationPolicy::Daily, RotationPolicy::Hourly, and
RotationPolicy::Period rotation policies, it may truncate the
contents of the existing file if the parameter is true, since the
file name is a time point and not an index.
This parameter is optional, and defaults to false.
Sourcepub fn capacity(self, capacity: usize) -> Self
pub fn capacity(self, capacity: usize) -> Self
Specifies the internal buffer capacity.
This parameter is optional, and defaults to the value consistent
with std.
Sourcepub fn level_filter(self, level_filter: LevelFilter) -> Self
pub fn level_filter(self, level_filter: LevelFilter) -> Self
Specifies a log level filter.
This parameter is optional, and defaults to LevelFilter::All.
Sourcepub fn formatter<F>(self, formatter: F) -> Selfwhere
F: Formatter + 'static,
pub fn formatter<F>(self, formatter: F) -> Selfwhere
F: Formatter + 'static,
Specifies a formatter.
This parameter is optional, and defaults to FullFormatter.
Sourcepub fn error_handler<F: Into<ErrorHandler>>(self, handler: F) -> Self
pub fn error_handler<F: Into<ErrorHandler>>(self, handler: F) -> Self
Specifies an error handler.
This parameter is optional, and defaults to
ErrorHandler::default().
Source§impl RotatingFileSinkBuilder<PathBuf, RotationPolicy>
impl RotatingFileSinkBuilder<PathBuf, RotationPolicy>
Sourcepub fn build(self) -> Result<RotatingFileSink>
pub fn build(self) -> Result<RotatingFileSink>
Builds a RotatingFileSink.
§Error
If the argument rotation_policy is invalid, or an error occurs opening
the file, Error::CreateDirectory or Error::OpenFile will be
returned.
Sourcepub fn build_arc(self) -> Result<Arc<RotatingFileSink>>
pub fn build_arc(self) -> Result<Arc<RotatingFileSink>>
Builds a Arc<RotatingFileSink>.
This is a shorthand method for .build().map(Arc::new).
Examples found in repository?
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, // 90 minutes
82 )))
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}